优化代码
This commit is contained in:
@@ -23,8 +23,8 @@ class OkHttpFactory private constructor() {
|
||||
val okHttpClient: OkHttpClient by lazy {
|
||||
OkHttpClient.Builder()
|
||||
.addNetworkInterceptor(HttpHeaderInterceptor())
|
||||
.addNetworkInterceptor(HttpLoggingInterceptor())
|
||||
.addInterceptor(HttpDnsInterceptor())
|
||||
.addInterceptor(HttpLoggingInterceptor())
|
||||
.sslSocketFactory(createSSLSocketFactory(), createTrustAllManager())
|
||||
.hostnameVerifier(SSLSocketFactoryUtils.TrustAllHostnameVerifier())
|
||||
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.mogo.utils.logger.Logger
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import java.lang.Exception
|
||||
|
||||
/**
|
||||
* created by wujifei on 2021/1/27 15:57
|
||||
@@ -13,14 +14,16 @@ import okhttp3.Response
|
||||
class HttpDnsInterceptor : Interceptor {
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val request: Request = chain.request()
|
||||
val path: String = request.url().encodedPath()
|
||||
val query: String? = request.url().encodedQuery()
|
||||
val host = "http://" + MogoHttpDnsClient.getHttpDnsAddressUseCacheIfNecessary(0, request.url().host().replace("http://", "").replace("https://", ""))
|
||||
var url = host + path
|
||||
query?.let {
|
||||
url=url+"?"+query
|
||||
var url = request.url().toString()
|
||||
val host = request.url().host()
|
||||
try {
|
||||
MogoHttpDnsClient.getHttpDnsAddressUseCacheIfNecessary(0, request.url().host())?.let {
|
||||
url = url.replace(host, it)
|
||||
Logger.d("DomainExchange", """oriHost: ${host} newHost: $it newUrl: $url""")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Logger.d("DomainExchange", e.toString())
|
||||
}
|
||||
Logger.d("DomainExchange", """oriHost: ${request.url().host().toString()} newHost: $host newUrl: $url""")
|
||||
return chain.proceed(request.newBuilder().url(url).build())
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* created by wujifei on 2021/1/27 14:33
|
||||
* describe:
|
||||
* describe:网络日志拦截
|
||||
*/
|
||||
class HttpLoggingInterceptor : Interceptor {
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
@@ -18,7 +18,7 @@ class HttpLoggingInterceptor : Interceptor {
|
||||
val requestBody = request.body()
|
||||
val hasRequestBody = requestBody != null
|
||||
var protocol = Protocol.HTTP_1_1.toString()
|
||||
var body = ""
|
||||
var body = "null"
|
||||
if (chain.connection() != null && chain.connection()!!.protocol() != null) {
|
||||
protocol = chain.connection()!!.protocol().toString()
|
||||
}
|
||||
@@ -44,8 +44,8 @@ class HttpLoggingInterceptor : Interceptor {
|
||||
val logMsg = StringBuilder()
|
||||
.append("------> http request start").append("\r\n")
|
||||
.append(protocol).append(", ")
|
||||
.append(request.method()).append(", ")
|
||||
.append("Request Headers: ").append(request.headers().toString()).append("\r\n")
|
||||
.append(request.method()).append("\r\n")
|
||||
.append("Request Headers: {").append(request.headers().toString().replace("\n",", ")).append("}\r\n")
|
||||
.append("Url: ").append(request.url()).append("\r\n")
|
||||
.append("Content-Type: ").append(requestBody?.contentType()).append("\r\n")
|
||||
.append("Content-Length: ").append(requestBody?.contentLength()).append("\r\n")
|
||||
@@ -78,7 +78,7 @@ class HttpLoggingInterceptor : Interceptor {
|
||||
.append("Response Content: ").append(responseContent).append("\r\n")
|
||||
.append("Content-Type: ").append(contentType).append("\r\n")
|
||||
.append("Content-Length: ").append(bodySize).append("\r\n")
|
||||
.append(" (").append(endTime).append("ms)").append("\r\n")
|
||||
.append("Time: (").append(endTime).append("ms)").append("\r\n")
|
||||
.append("------> http response end")
|
||||
Logger.d(TAG, logMsg.toString())
|
||||
return if (consumedResponse) response.newBuilder().body(ResponseBody.create(contentType, responseContent)).build() else response
|
||||
|
||||
Reference in New Issue
Block a user