diff --git a/README.md b/README.md index 670047c..3e8d66f 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Step 2. Add the dependency ``` dependencies { - implementation 'com.github.zeropercenthappy:RetrofitUtils:1.0.7' + implementation 'com.github.zeropercenthappy:RetrofitUtils:1.0.8' } ``` diff --git a/retrofitLib/build.gradle b/retrofitLib/build.gradle index 019f92f..9370cde 100644 --- a/retrofitLib/build.gradle +++ b/retrofitLib/build.gradle @@ -10,8 +10,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 28 - versionCode 107 - versionName "1.0.7" + versionCode 108 + versionName "1.0.8" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/CookieManager.kt b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/CookieManager.kt index 9436229..fed591a 100644 --- a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/CookieManager.kt +++ b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/CookieManager.kt @@ -5,46 +5,51 @@ import android.text.TextUtils import retrofit2.Response object CookieManager { - const val CONSTANT_SET_COOKIE = "Set-Cookie" + + private const val CONSTANT_SET_COOKIE = "Set-Cookie" const val CONSTANT_COOKIE = "Cookie" @JvmStatic - fun getCookie(context: Context): String { + fun getCookie(context: Context, protocol: String, domain: String, port: Int): String { context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE).run { - return getString(CONSTANT_COOKIE, "") ?: "" + return getString("${CONSTANT_COOKIE}_${protocol}_${domain}_${port}", "") ?: "" } } @JvmStatic - private fun setCookie(context: Context, cookie: String) { + private fun setCookie(context: Context, protocol: String, domain: String, port: Int, cookie: String) { context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE).edit().run { - putString(CONSTANT_COOKIE, cookie) + putString("${CONSTANT_COOKIE}_${protocol}_${domain}_${port}", cookie) apply() } } @JvmStatic - fun updateCookie(context: Context, response: Response<*>) { - response.headers()?.get(CONSTANT_SET_COOKIE)?.run { - if (!TextUtils.isEmpty(this)) { - setCookie(context, this) - } + fun updateCookie(context: Context, protocol: String, domain: String, port: Int, response: Response<*>) { + val cookieSB = StringBuilder() + for (cookie in response.headers().values(CONSTANT_SET_COOKIE)) { + cookieSB.append(cookie).append("; ") + } + if (cookieSB.isNotEmpty()) { + setCookie(context, protocol, domain, port, cookieSB.toString()) } } @JvmStatic - fun updateCookie(context: Context, response: okhttp3.Response) { - response.headers[CONSTANT_SET_COOKIE]?.run { - if (!TextUtils.isEmpty(this)) { - setCookie(context, this) - } + fun updateCookie(context: Context, protocol: String, domain: String, port: Int, response: okhttp3.Response) { + val cookieSB = StringBuilder() + for (cookie in response.headers(CONSTANT_SET_COOKIE)) { + cookieSB.append(cookie).append("; ") + } + if (cookieSB.isNotEmpty()) { + setCookie(context, protocol, domain, port, cookieSB.toString()) } } @JvmStatic - fun updateCookie(context: Context, cookie: String) { + fun updateCookie(context: Context, protocol: String, domain: String, port: Int, cookie: String) { if (TextUtils.isEmpty(cookie)) { - setCookie(context, cookie) + setCookie(context, protocol, domain, port, cookie) } } } \ No newline at end of file diff --git a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/DefaultInterceptor.kt b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/DefaultInterceptor.kt index 228f2f6..67616a9 100644 --- a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/DefaultInterceptor.kt +++ b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/DefaultInterceptor.kt @@ -4,9 +4,11 @@ import android.content.Context import android.text.TextUtils import okhttp3.* -class DefaultInterceptor(private var context: Context, private val handleCookie: Boolean = true, - private val extraParamMap: Map, - private val extraHeaderMap: Map) : Interceptor { +class DefaultInterceptor( + private var context: Context, private val handleCookie: Boolean = true, + private val extraParamMap: Map, + private val extraHeaderMap: Map +) : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { var request = chain.request() @@ -17,7 +19,7 @@ class DefaultInterceptor(private var context: Context, private val handleCookie: val requestBuilder = request.newBuilder() //是否需要自动管理Cookie if (handleCookie) { - val cookie = CookieManager.getCookie(context) + val cookie = CookieManager.getCookie(context, request.url.scheme, request.url.host, request.url.port) if (!TextUtils.isEmpty(cookie)) { requestBuilder.addHeader(CookieManager.CONSTANT_COOKIE, cookie) } @@ -32,7 +34,7 @@ class DefaultInterceptor(private var context: Context, private val handleCookie: val response = chain.proceed(requestBuilder.build()) //处理返回的Cookie - CookieManager.updateCookie(context, response) + CookieManager.updateCookie(context, request.url.scheme, request.url.host, request.url.port, response) return response } @@ -68,8 +70,8 @@ class DefaultInterceptor(private var context: Context, private val handleCookie: } val formBody = formBodyBuilder.build() return request.newBuilder() - .method(request.method, formBody) - .build() + .method(request.method, formBody) + .build() } is MultipartBody -> { //MultipartBody @@ -88,8 +90,8 @@ class DefaultInterceptor(private var context: Context, private val handleCookie: } val multipartBody = builder.build() return request.newBuilder() - .method(request.method, multipartBody) - .build() + .method(request.method, multipartBody) + .build() } else -> return request }