OKhttp源码解析---拦截器之CallServerInterceptor
2016-11-02 18:04
429 查看
CallServerInterceptor负责发送请求和获取响应,我们看下它的intercept
@Override public Response intercept(Chain chain) throws IOException {
HttpStream httpStream = ((RealInterceptorChain) chain).httpStream();
StreamAllocation streamAllocation = ((RealInterceptorChain) chain).streamAllocation();
Request request = chain.request();
long sentRequestMillis = System.currentTimeMillis();
httpStream.writeRequestHeaders(request);
if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
Sink requestBodyOut = httpStream.createRequestBody(request, request.body().contentLength());
BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);
request.body().writeTo(bufferedRequestBody);
bufferedRequestBody.close();
}
httpStream.finishRequest();
Response response = httpStream.readResponseHeaders()
.request(request)
.handshake(streamAllocation.connection().handshake())
.sentRequestAtMillis(sentRequestMillis)
.receivedResponseAtMillis(System.currentTimeMillis())
.build();
if (!forWebSocket || response.code() != 101) {
response = response.newBuilder()
.body(httpStream.openResponseBody(response))
.build();
}
if ("close".equalsIgnoreCase(response.request().header("Connection"))
|| "close".equalsIgnoreCase(response.header("Connection"))) {
streamAllocation.noNewStreams();
}
int code = response.code();
if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
throw new ProtocolException(
"HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
}
return response;
}1、首先会写请求头
@Override public void writeRequestHeaders(Request request) throws IOException {
String requestLine = RequestLine.get(
request, streamAllocation.connection().route().proxy().type());
writeRequest(request.headers(), requestLine);
}
2、查看是否运行当前方法带body,运行则写body
3、调用httpStream.finishRequest();刷新
@Override public void finishRequest() throws IOException {
sink.flush();
}
4、读取响应头和body并设置Response
5、是否需要关闭连接
6、返回Response
@Override public Response intercept(Chain chain) throws IOException {
HttpStream httpStream = ((RealInterceptorChain) chain).httpStream();
StreamAllocation streamAllocation = ((RealInterceptorChain) chain).streamAllocation();
Request request = chain.request();
long sentRequestMillis = System.currentTimeMillis();
httpStream.writeRequestHeaders(request);
if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
Sink requestBodyOut = httpStream.createRequestBody(request, request.body().contentLength());
BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);
request.body().writeTo(bufferedRequestBody);
bufferedRequestBody.close();
}
httpStream.finishRequest();
Response response = httpStream.readResponseHeaders()
.request(request)
.handshake(streamAllocation.connection().handshake())
.sentRequestAtMillis(sentRequestMillis)
.receivedResponseAtMillis(System.currentTimeMillis())
.build();
if (!forWebSocket || response.code() != 101) {
response = response.newBuilder()
.body(httpStream.openResponseBody(response))
.build();
}
if ("close".equalsIgnoreCase(response.request().header("Connection"))
|| "close".equalsIgnoreCase(response.header("Connection"))) {
streamAllocation.noNewStreams();
}
int code = response.code();
if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
throw new ProtocolException(
"HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
}
return response;
}1、首先会写请求头
@Override public void writeRequestHeaders(Request request) throws IOException {
String requestLine = RequestLine.get(
request, streamAllocation.connection().route().proxy().type());
writeRequest(request.headers(), requestLine);
}
public void writeRequest(Headers headers, String requestLine) throws IOException { if (state != STATE_IDLE) throw new IllegalStateException("state: " + state); sink.writeUtf8(requestLine).writeUtf8("\r\n"); for (int i = 0, size = headers.size(); i < size; i++) { sink.writeUtf8(headers.name(i)) .writeUtf8(": ") .writeUtf8(headers.value(i)) .writeUtf8("\r\n"); } sink.writeUtf8("\r\n"); state = STATE_OPEN_REQUEST_BODY; }
2、查看是否运行当前方法带body,运行则写body
3、调用httpStream.finishRequest();刷新
@Override public void finishRequest() throws IOException {
sink.flush();
}
4、读取响应头和body并设置Response
5、是否需要关闭连接
6、返回Response
相关文章推荐
- OkHttp3源码解析02-拦截器
- OkHttp 3.x 源码解析之Interceptor 拦截器
- Android OkHttp 源码解析 (五) CacheInterceptor 缓存拦截器
- OKhttp源码解析---拦截器之BridgeInterceptor
- OkHttp3.0源码解析---拦截器
- OKhttp源码解析---拦截器之CacheInterceptor
- OkHttp 源码解析(3) Interceptor 拦截器
- [置顶] 【Android okhttp源码解析 五】拦截器流程和源码解析
- OkHttp中的拦截器机制源码解析
- Android OkHttp 源码解析 (四) Interceptor 应用层拦截器、网络层拦截器
- Okhttp源码解析之Interceptor(拦截器)
- OKhttp源码解析---拦截器之RetryAndFollowUpInterceptor
- OKhttp源码解析---拦截器之ConnectInterceptor
- OKHttp源码解析(二)
- OKHttp使用详解及源码解析
- OKHttp源码解析
- OKHttp源码解析(一)
- OKHttp源码解析(三)
- One Step By One Step 解析OkHttp3 - RealCall (二)
- OkHttp源码解析(1)