OkHttp使用彻底教程

  1. 历史上Http请求库优缺点
    在讲述OkHttp以前, 咱们看下没有OkHttp的时代, 咱们是如何完成http请求的.php

在没有OkHttp的日子, 咱们使用 HttpURLConnection 或者 HttpClient . 那么这二者都有什么优缺点呢? 为何不在继续使用下去呢?
HttpClient 是Apache基金会的一个开源网络库, 功能十分强大, API数量众多, 可是正是因为庞大的API数量使得咱们很难在不破坏兼容性的状况下对它进行升级和扩展, 因此Android团队在提高和优化HttpClient方面的工做态度并不积极.
HttpURLConnection 是一种多用途, 轻量极的HTTP客户端, 提供的API比较简单, 能够容易地去使用和扩展. 不过在Android 2.2版本以前, HttpURLConnection 一直存在着一些使人厌烦的bug. 好比说对一个可读的InputStream调用close()方法时,就有可能会致使链接池失效了。那么咱们一般的解决办法就是直接禁用掉链接池的功能:
private void disableConnectionReuseIfNecessary() {html

// 这是一个2.2版本以前的bug    
if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {    
    System.setProperty("http.keepAlive", "false");    
}

}
所以, 通常的推荐是在2.2以前, 使用 HttpClient , 由于其bug较少. 在2.2以后, 推荐使用 HttpURLConnection , 由于API简单, 体积小, 而且有压缩和缓存机制, 而且Android团队后续会继续优化 HttpURLConnection .
可是, 上面两个类库和 OkHttp 比起来就弱爆了, 由于OkHttp不只具备高效的请求效率, 而且提供了不少开箱即用的网络疑难杂症解决方案.java

  • 支持HTTP/2, HTTP/2经过使用多路复用技术在一个单独的TCP链接上支持并发, 经过在一个链接上一次性发送多个请求来发送或接收数据git

  • 若是HTTP/2不可用, 链接池复用技术也能够极大减小延时github

  • 支持GZIP, 能够压缩下载体积web

  • 响应缓存能够直接避免重复请求apache

  • 会从不少经常使用的链接问题中自动恢复json

  • 若是您的服务器配置了多个IP地址, 当第一个IP链接失败的时候, OkHttp会自动尝试下一个IPapi

  • OkHttp还处理了代理服务器问题和SSL握手失败问题
    使用 OkHttp 无需重写您程序中的网络代码。OkHttp实现了几乎和java.net.HttpURLConnection同样的API。若是你用了 Apache HttpClient,则OkHttp也提供了一个对应的okhttp-apache 模块。数组

还有一个好消息, 从Android 4.4起, 其 HttpURLConnection 的内部实现已经变为 OkHttp , 您能够参考这两个网页: 爆栈网 和 Twitter .

  1. OkHttp类与http请求响应的映射
    在讲解OkHttp使用以前, 再看下咱们Http请求和响应都有哪些部分组成.

2.1 http请求

因此一个类库要完成一个http请求, 须要包含 请求方法 , 请求地址 , 请求协议 , 请求头 , 请求体 这五部分. 这些都在 okhttp3.Request 的类中有体现, 这个类正是表明http请求的类. 看下图:

其中 HttpUrl 类表明 请求地址 , String method 表明 请求方法 , Headers 表明请求头, RequestBody 表明请求体. Object tag 这个是用来取消http请求的标志, 这个咱们先无论. 这里也许你在疑惑, 请求协议 呢? 为何没有请求协议对应的类. 且听我慢慢道来, 下面就会讲到这个问题.
2.1.1 请求协议的协商升级
目前, Http/1.1在全世界大范围的使用中, 直接废弃跳到http/2确定不现实. 不是每一个用户的浏览器都支持http/2的, 也不是每一个服务器都打算支持http/2的, 若是咱们直接发送http/2格式的协议, 服务器又不支持, 那不是挂掉了! 总不能维护一个全世界的网站列表, 表示哪些支持http/2, 哪些不支持?
为了解决这个问题, 从稍高层次上来讲, 就是为了更方便地部署新协议, HTTP/1.1 引入了 Upgrade 机制. 这个机制在 RFC7230 的「 6.7 Upgrade 」这一节中有详细描述.
简单说来, 就是先问下你支持http/2么? 若是你支持, 那么接下来我就用http/2和你聊天. 若是你不支持, 那么我仍是用原来的http/1.1和你聊天.
1.客户端在请求头部中指定 Connection 和 Upgrade 两个字段发起 HTTP/1.1 协议升级. HTTP/2 的协议名称是 h2c, 表明 HTTP/2 ClearText.
GET / HTTP/1.1
Host: example.com
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload>
2.若是服务端不一样意升级或者不支持 Upgrade 所列出的协议,直接忽略便可(当成 HTTP/1.1 请求,以 HTTP/1.1 响应).
HTTP/1.1 200 OK
Content-Length: 243
Content-Type: text/html

...
若是服务端赞成升级,那么须要这样响应:
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: h2c

[ HTTP/2 connection ... ]
HTTP Upgrade 响应的状态码是 101,而且响应正文可使用新协议定义的数据格式。
这样就能够完成从http/1.1升级到http/2了. 一样也能够从http/1.1升级到WebSocket.
这样, 你就了解了为何OkHttp没有指定具体请求协议了吧. 由于OkHttp使用了请求协议的协商升级, 不管是1.1仍是2, 都先只以1.1来发送, 并在发送的信息头里包含协议升级字段. 接下来就看服务器是否支持协议升级了. OkHttp使用的协议升级字段是 ALPN , 若是有兴趣, 能够更深刻的查阅相关资料.
2.1.2 OkHttp请求
接下来咱们构造一个http请求, 并查看请求具体内容.
final Request request = new Request.Builder().url("https://github.com/").build();
咱们看下在内存中, 这个请求是什么样子的, 是否如咱们上文所说和 请求方法 , 请求地址 , 请求头 , 请求体 一一对应.

2.2 http响应
咱们看下一个http响应由哪些部分组成, 先看下响应组成图:

能够看到大致由 应答首行 , 应答头 , 应答体 构成. 可是 应答首行 表达的信息过多, HTTP/1.1 表示 访问协议 , 200 是响应码, OK 是描述状态的消息. 根据单一职责, 咱们不该该把这么多内容用一个 应答首行 来表示. 这样的话, 咱们的响应就应该由 访问协议 , 响应码 , 描述信息 , 响应头 , 响应体 来组成.
2.2.1 OkHttp响应
咱们看下OkHttp库怎么表示一个响应:

能够看到 Response 类里面有 Protocol 表明 请求协议 , int code 表明 响应码 , String message 表明 描述信息 , Headers 表明 响应头 , ResponseBody 表明 响应体 . 固然除此以外, 还有 Request 表明持有的请求, Handshake 表明SSL/TLS握手协议验证时的信息, 这些额外信息咱们暂时不问.
有了刚才说的OkHttp响应的类组成, 咱们看下OkHttp请求后响应在内存中的内容:
final Request request = new Request.Builder().url("https://github.com/").build();
Response response = client.newCall(request).execute();
能够看到和咱们的分析十分一致.
讲了OkHttp里的请求类和响应类, 咱们接下来就能够直接讲述OkHttp的使用方法了.
3 HTTP GET
3.1 同步GET
同步GET的意思是一直等待http请求, 直到返回了响应. 在这之间会阻塞进程, 因此经过get不能在Android的主线程中执行, 不然会报错.
private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

Request request = new Request.Builder()
    .url("http://publicobject.com/helloworld.txt")
    .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
  System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}

System.out.println(response.body().string());

}
OkHttpClient实现了 Call.Factory 接口, 是Call的工厂类, Call负责发送执行请求和读取响应.
Request表明Http请求, 经过Request.Builder辅助类来构建.
client.newCall(request)经过传入一个http request, 返回一个Call调用. 而后执行execute()方法, 同步得到
Response表明Http请求的响应. response.body()是ResponseBody类, 表明响应体, 能够经过responseBody.string()得到字符串的表达形式, 或responseBody.bytes()得到字节数组的表达形式, 这两种形式都会把文档加入到内存. 也能够经过responseBody.charStream()和responseBody.byteStream()返回流来处理.
上述代码完成的功能是下载一个文件, 打印他的响应头, 以string形式打印响应体.
响应体的string()方法对于小文档来讲十分方便高效. 可是若是响应体太大(超过1MB), 应避免使用 string()方法, 由于它会将把整个文档加载到内存中.
对于超过1MB的响应body, 应使用流的方式来处理响应body. 这和咱们处理xml文档的逻辑是一致的, 小文件能够载入内存树状解析, 大文件就必须流式解析.
3.2 异步GET
异步GET是指在另外的工做线程中执行http请求, 请求时不会阻塞当前的线程, 因此能够在Android主线程中使用.
下面是在一个工做线程中下载文件, 当响应可读时回调Callback接口. 当响应头准备好后, 就会调用Callback接口, 因此读取 响应体 时可能会阻塞. OkHttp现阶段不提供异步api来接收响应体。
private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

Request request = new Request.Builder()
    .url("http://publicobject.com/helloworld.txt")
    .build();

client.newCall(request).enqueue(new Callback() {
  @Override public void onFailure(Request request, Throwable throwable) {
    throwable.printStackTrace();
  }

  @Override public void onResponse(Response response) throws IOException {
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    Headers responseHeaders = response.headers();
    for (int i = 0; i < responseHeaders.size(); i++) {
      System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
    }

    System.out.println(response.body().string());
  }
});

}
4 HTTP POST
4.1 Post方式提交String
下面是使用HTTP POST提交请求到服务. 这个例子提交了一个markdown文档到web服务, 以HTML方式渲染markdown. 由于整个请求体都在内存中, 所以避免使用此api提交大文档(大于1MB).
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

String postBody = ""
    + "Releases\n"
    + "--------\n"
    + "\n"
    + " * _1.0_ May 6, 2013\n"
    + " * _1.1_ June 15, 2013\n"
    + " * _1.2_ August 11, 2013\n";

Request request = new Request.Builder()
    .url("https://api.github.com/markdown/raw")
    .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
    .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());

}
4.2 Post方式提交流
以流的方式POST提交请求体. 请求体的内容由流写入产生. 这个例子是流直接写入Okio的BufferedSink. 你的程序可能会使用OutputStream, 你可使用BufferedSink.outputStream()来获取. OkHttp的底层对流和字节的操做都是基于Okio库, Okio库也是Square开发的另外一个IO库, 填补I/O和NIO的空缺, 目的是提供简单便于使用的接口来操做IO.
public static final MediaType MEDIA_TYPE_MARKDOWN

= MediaType.parse("text/x-markdown; charset=utf-8");

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

RequestBody requestBody = new RequestBody() {
  @Override public MediaType contentType() {
    return MEDIA_TYPE_MARKDOWN;
  }

  @Override public void writeTo(BufferedSink sink) throws IOException {
    sink.writeUtf8("Numbers\n");
    sink.writeUtf8("-------\n");
    for (int i = 2; i <= 997; i++) {
      sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i)));
    }
  }

  private String factor(int n) {
    for (int i = 2; i < n; i++) {
      int x = n / i;
      if (x * i == n) return factor(x) + " × " + i;
    }
    return Integer.toString(n);
  }
};

Request request = new Request.Builder()
    .url("https://api.github.com/markdown/raw")
    .post(requestBody)
    .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());

}
4.3 Post方式提交文件
以文件做为请求体是十分简单的。
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

File file = new File("README.md");

Request request = new Request.Builder()
    .url("https://api.github.com/markdown/raw")
    .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
    .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());

}
4.4 Post方式提交表单
使用FormEncodingBuilder来构建和HTML <form> 标签相同效果的请求体. 键值对将使用一种HTML兼容形式的URL编码来进行编码.
private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

RequestBody formBody = new FormBody.Builder()
    .add("search", "Jurassic Park")
    .build();
Request request = new Request.Builder()
    .url("https://en.wikipedia.org/w/index.php")
    .post(formBody)
    .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());

}
4.5 Post方式提交分块请求
MultipartBody.Builder能够构建复杂的请求体, 与HTML文件上传形式兼容. 多块请求体中每块请求都是一个请求体, 能够定义本身的请求头. 这些请求头能够用来描述这块请求, 例如它的Content-Disposition. 若是Content-Length和Content-Type可用的话, 他们会被自动添加到请求头中.
private static final String IMGUR_CLIENT_ID = "...";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

// Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
RequestBody requestBody = new MultipartBody.Builder()
    .setType(MultipartBody.FORM)
    .addFormDataPart("title", "Square Logo")
    .addFormDataPart("image", "logo-square.png",
        RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
    .build();

Request request = new Request.Builder()
    .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
    .url("https://api.imgur.com/3/image")
    .post(requestBody)
    .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());

}

  1. 其余用法
    5.1 提取响应头

典型的HTTP头像是一个 Map<String, String> : 每一个字段都有一个或没有值. 可是一些头容许多个值, 像Guava的Multimap.
例如: HTTP响应里面提供的Vary响应头, 就是多值的. OkHttp的api试图让这些状况都适用.
当写请求头的时候, 使用header(name, value)能够设置惟一的name、value. 若是已经有值, 旧的将被移除, 而后添加新的. 使用addHeader(name, value)能够添加多值(添加, 不移除已有的).
当读取响应头时, 使用header(name)返回最后出现的name、value. 一般状况这也是惟一的name、value. 若是没有值, 那么header(name)将返回null. 若是想读取字段对应的全部值, 使用headers(name)会返回一个list.
为了获取全部的Header, Headers类支持按index访问.
private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

Request request = new Request.Builder()
    .url("https://api.github.com/repos/square/okhttp/issues")
    .header("User-Agent", "OkHttp Headers.java")
    .addHeader("Accept", "application/json; q=0.5")
    .addHeader("Accept", "application/vnd.github.v3+json")
    .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println("Server: " + response.header("Server"));
System.out.println("Date: " + response.header("Date"));
System.out.println("Vary: " + response.headers("Vary"));

}
5.2 使用Gson来解析JSON响应
Gson是一个在JSON和Java对象之间转换很是方便的api库. 这里咱们用Gson来解析Github API的JSON响应.
注意: ResponseBody.charStream()使用响应头Content-Type指定的字符集来解析响应体. 默认是UTF-8.
private final OkHttpClient client = new OkHttpClient();
private final Gson gson = new Gson();

public void run() throws Exception {

Request request = new Request.Builder()
    .url("https://api.github.com/gists/c2a7c39532239ff261be")
    .build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

Gist gist = gson.fromJson(response.body().charStream(), Gist.class);
for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) {
  System.out.println(entry.getKey());
  System.out.println(entry.getValue().content);
}

}

static class Gist {

Map<String, GistFile> files;

}

static class GistFile {

String content;

}
5.3 响应缓存
为了缓存响应, 你须要一个你能够读写的缓存目录, 和缓存大小的限制. 这个缓存目录应该是私有的, 不信任的程序应不能读取缓存内容.
一个缓存目录同时拥有多个缓存访问是错误的. 大多数程序只须要调用一次new OkHttp(), 在第一次调用时配置好缓存, 而后其余地方只须要调用这个实例就能够了. 不然两个缓存示例互相干扰, 破坏响应缓存, 并且有可能会致使程序崩溃.
响应缓存使用HTTP头做为配置. 你能够在请求头中添加Cache-Control: max-stale=3600 , OkHttp缓存会支持. 你的服务经过响应头肯定响应缓存多长时间, 例如使用Cache-Control: max-age=9600.
private final OkHttpClient client;

public CacheResponse(File cacheDirectory) throws Exception {

int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(cacheDirectory, cacheSize);

client = new OkHttpClient();
client.setCache(cache);

}

public void run() throws Exception {

Request request = new Request.Builder()
    .url("http://publicobject.com/helloworld.txt")
    .build();

Response response1 = client.newCall(request).execute();
if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1);

String response1Body = response1.body().string();
System.out.println("Response 1 response:          " + response1);
System.out.println("Response 1 cache response:    " + response1.cacheResponse());
System.out.println("Response 1 network response:  " + response1.networkResponse());

Response response2 = client.newCall(request).execute();
if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2);

String response2Body = response2.body().string();
System.out.println("Response 2 response:          " + response2);
System.out.println("Response 2 cache response:    " + response2.cacheResponse());
System.out.println("Response 2 network response:  " + response2.networkResponse());

System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body));

}
若是须要阻值response使用缓存, 使用 CacheControl.FORCE_NETWORK . 若是须要阻值response使用网络, 使用 CacheControl.FORCE_CACHE .
警告: 若是你使用 FORCE_CACHE , 可是response要求使用网络, OkHttp将会返回一个 504 Unsatisfiable Request 响应.
5.3.1 Force a Network Response
有些时候, 好比用户刚刚点击 刷新 按钮, 这时必须跳过缓存, 直接从服务器抓取数据. 为了强制全面刷新, 咱们须要添加 no-cache 指令:
connection.addRequestProperty("Cache-Control", "no-cache");
这样就能够强制每次请求直接发送给源服务器, 而不通过本地缓存版本的校验, 经常使用于须要确认认证的应用和严格要求使用最新数据的应用.
5.3.2 Force a Cache Response
有时你会想当即显示资源. 这样即便在后台正下载着最新资源, 你的客户端仍然能够先显示原有资源, 毕竟有个东西显示比没有东西显示要好.
若是须要限制让请求优先使用本地缓存资源, 须要增长 only-if-cached 指令:
try {

connection.addRequestProperty("Cache-Control", "only-if-cached");
 InputStream cached = connection.getInputStream();
 // the resource was cached! show it

catch (FileNotFoundException e) {

// the resource was not cached

}
}
5.4 取消一个Call
使用Call.cancel()能够当即中止掉一个正在执行的call. 若是一个线程正在写请求或者读响应, 将会引起IOException. 当call没有必要的时候, 使用这个api能够节约网络资源. 例如当用户离开一个应用时, 无论同步仍是异步的call均可以取消.
你能够经过tags来同时取消多个请求. 当你构建一请求时, 使用RequestBuilder.tag(tag)来分配一个标签, 以后你就能够用OkHttpClient.cancel(tag)来取消全部带有这个tag的call.
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

Request request = new Request.Builder()
    .url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay.
    .build();

final long startNanos = System.nanoTime();
final Call call = client.newCall(request);

// Schedule a job to cancel the call in 1 second.
executor.schedule(new Runnable() {
  @Override public void run() {
    System.out.printf("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f);
    call.cancel();
    System.out.printf("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f);
  }
}, 1, TimeUnit.SECONDS);

try {
  System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f);
  Response response = call.execute();
  System.out.printf("%.2f Call was expected to fail, but completed: %s%n",
      (System.nanoTime() - startNanos) / 1e9f, response);
} catch (IOException e) {
  System.out.printf("%.2f Call failed as expected: %s%n",
      (System.nanoTime() - startNanos) / 1e9f, e);
}

}
5.5 超时
没有响应时使用超时结束call. 没有响应的缘由多是客户点连接问题、服务器可用性问题或者这之间的其余东西. OkHttp支持链接超时, 读取超时和写入超时.
private final OkHttpClient client;

public ConfigureTimeouts() throws Exception {

client = new OkHttpClient.Builder()
    .connectTimeout(10, TimeUnit.SECONDS)
    .writeTimeout(10, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build();

}

public void run() throws Exception {

Request request = new Request.Builder()
    .url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay.
    .build();

Response response = client.newCall(request).execute();
System.out.println("Response completed: " + response);

}
5.6 每一个call的配置
使用OkHttpClient, 全部的HTTP Client配置包括代理设置、超时设置、缓存设置. 当你须要为单个call改变配置的时候, 调用 OkHttpClient.newBuilder() . 这个api将会返回一个builder, 这个builder和原始的client共享相同的链接池, 分发器和配置.
下面的例子中,咱们让一个请求是500ms的超时、另外一个是3000ms的超时。
private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

Request request = new Request.Builder()
    .url("http://httpbin.org/delay/1") // This URL is served with a 1 second delay.
    .build();

try {
  // Copy to customize OkHttp for this request.
  OkHttpClient copy = client.newBuilder()
      .readTimeout(500, TimeUnit.MILLISECONDS)
      .build();

  Response response = copy.newCall(request).execute();
  System.out.println("Response 1 succeeded: " + response);
} catch (IOException e) {
  System.out.println("Response 1 failed: " + e);
}

try {
  // Copy to customize OkHttp for this request.
  OkHttpClient copy = client.newBuilder()
      .readTimeout(3000, TimeUnit.MILLISECONDS)
      .build();

  Response response = copy.newCall(request).execute();
  System.out.println("Response 2 succeeded: " + response);
} catch (IOException e) {
  System.out.println("Response 2 failed: " + e);
}

}
5.7 处理验证
这部分和HTTP AUTH有关.
5.7.1 HTTP AUTH
使用HTTP AUTH须要在server端配置http auth信息, 其过程以下:

  • 客户端发送http请求

  • 服务器发现配置了http auth, 因而检查request里面有没有”Authorization”的http header

  • 若是有, 则判断Authorization里面的内容是否在用户列表里面, Authorization header的典型数据为”Authorization: Basic jdhaHY0=”, 其中Basic表示基础认证, jdhaHY0=是base64编码的”user:passwd”字符串. 若是没有,或者用户密码不对,则返回http code 401页面给客户端.

  • 标准的http浏览器在收到401页面以后, 应该弹出一个对话框让用户输入账号密码; 并在用户点确认的时候再次发出请求, 此次请求里面将带上Authorization header.
    一次典型的访问场景是:

浏览器发送http请求(没有Authorization header)
服务器端返回401页面
浏览器弹出认证对话框
用户输入账号密码,并点确认
浏览器再次发出http请求(带着Authorization header)
服务器端认证经过,并返回页面
浏览器显示页面
5.7.2 OkHttp认证
OkHttp会自动重试未验证的请求. 当响应是 401 Not Authorized 时, Authenticator 会被要求提供证书. Authenticator的实现中须要创建一个新的包含证书的请求. 若是没有证书可用, 返回null来跳过尝试.
使用 Response.challenges() 来得到任何 authentication challenges 的 schemes 和 realms. 当完成一个 Basic challenge , 使用 Credentials.basic(username, password) 来解码请求头.
private final OkHttpClient client;

public Authenticate() {

client = new OkHttpClient.Builder()
    .authenticator(new Authenticator() {
      @Override public Request authenticate(Route route, Response response) throws IOException {
        System.out.println("Authenticating for response: " + response);
        System.out.println("Challenges: " + response.challenges());
        String credential = Credentials.basic("jesse", "password1");
        return response.request().newBuilder()
            .header("Authorization", credential)
            .build();
      }
    })
    .build();

}

public void run() throws Exception {

Request request = new Request.Builder()
    .url("http://publicobject.com/secrets/hellosecret.txt")
    .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());

}
当认证没法工做时, 为了不屡次重试, 你能够返回空来放弃认证. 例如, 当 exact credentials 已经尝试过, 你可能会直接想跳过认证, 能够这样作:
if (credential.equals(response.request().header("Authorization"))) {

return null; // If we already failed with these credentials, don't retry.

}
当重试次数超过定义的次数, 你若想跳过认证, 能够这样作:
if (responseCount(response) >= 3) {

return null; // If we've failed 3 times, give up.

}

private int responseCount(Response response) {

int result = 1;
while ((response = response.priorResponse()) != null) {
  result++;
}
return result;

}

相关文章
相关标签/搜索