123456789101112131415161718192021222324final Request.Builder builder = new Request.Builder().url(url)builder.addHeader(key,value) //将请求头以键值对形式添加,可添加多个请求头:
final Request request = builder.build()final OkHttpClient client = new OkHttpClient.Builder() .readTimeout(30, TimeUnit.SECONDS) .connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .build() //设置各种超时时间final Call call = client.newCall(request)new Thread(new Runnable() { @Override public void run() { try { Response response = call.execute() if (response != null) { } else { } } catch (IOException e) { e.printStackTrace() } }}).start()
HTTPS在传输数据之前需要客户端(浏览器)与服务端(网站)之间进行一次握手,在握手过程中将确立双方加密传输数据的密码信息。握手过程的简单描述如下:
浏览器将自己支持的一套加密算法、HASH算法发送给网站。
1.网站从中选出一组加密算法与HASH算法,并将自己的身份信息以证书的形式发回给浏览器。证书里面包含了网站地址,加密公钥,以及证书的颁发机构等信息。
2.浏览器获得网站证书之后,开始验证证书的合法性,如果证书信任,则生成一串随机数字作为通讯过程中对称加密的秘钥。然后取出证书中的公钥,将这串数字以及HASH的结果进行加密,然后发给网站。
3.网站接收浏览器发来的数据之后,通过私钥进行解密,然后HASH校验,如果一致,则使用浏览器发来的数字串使加密一段握手消息发给浏览器。
4.浏览器解密,并HASH校验,没有问题,则握手结束。接下来的传输过程将由之前浏览器生成的随机密码并利用对称加密算法进行加密。
原文: https://blog.csdn.net/sk719887916/article/details/52189602
添加统一通用header, 不会覆盖前面的header
okHttpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request()
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.addHeader("header-key", "value1")
.addHeader("header-key", "value2")
Request request = requestBuilder.build()
return chain.proceed(request)
}
})
添加统一通用header, 会覆盖前面的header
okHttpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request()
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("headerkey", "header-value")// <-- this is the important line
Request request = requestBuilder.build()
return chain.proceed(request)
}
})
统一添加参数
OkHttpClient.Builder httpClient =
new OkHttpClient.Builder()
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request()
HttpUrl originalHttpUrl = original.url()
HttpUrl url = originalHttpUrl.newBuilder()
.addQueryParameter("apikey", "your-actual-api-key")
.build()
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.url(url)
Request request = requestBuilder.build()
return chain.proceed(request)
}
})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)