本文整理汇总了Java中com.squareup.okhttp.apache.OkApacheClient类的典型用法代码示例。如果您正苦于以下问题:Java OkApacheClient类的具体用法?Java OkApacheClient怎么用?Java OkApacheClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OkApacheClient类属于com.squareup.okhttp.apache包,在下文中一共展示了OkApacheClient类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setupHttpClient
import com.squareup.okhttp.apache.OkApacheClient; //导入依赖的package包/类
static private HttpClient setupHttpClient() {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(30, TimeUnit.SECONDS);
client.setFollowRedirects(false);
OkApacheClient apacheClient = new OkApacheClient(client);
return apacheClient;
}
示例2: apacheAlikeApiSample
import com.squareup.okhttp.apache.OkApacheClient; //导入依赖的package包/类
private static void apacheAlikeApiSample() throws Exception {
System.out.println("::: It's a apache client alike api usage.");
// ::: It's a apache client alike api usage.
// Create a apache kind of http client which you can share in whole application
OkApacheClient client = new OkApacheClient();
client.impl().setHostnameVerifier(UTILS.createInsecureHostnameVerifier());
client.impl().setSslSocketFactory(UTILS.createInsecureSslSocketFactory());
// Config the preferred protocol for special host you prefer to invoke with special protocol.
client.configPreferredHostProtocol("118.186.217.31", Protocol.SPDY_3);
// http://118.186.217.31 hosts with a spdy style http service, we create a get request for it
HttpGet request = new HttpGet(new URL("http://118.186.217.31/").toURI());
// HttpGet request = new HttpGet(new URL("https://www.cloudflare.com/").toURI());
// then run it.
HttpResponse response = client.execute(request);
// should OUTPUT:
// HTTP/1.1 200 OK [OkHttp-Selected-Protocol: spdy/3.1, server: nginx/1.5.11, date: \
// Thu, 12 Jun 2014 09:09:34 GMT, content-type: text/html; charset=UTF-8, alternate-protocol:\
// 443:npn-spdy/3, OkHttp-Sent-Millis: 1402564173672, OkHttp-Received-Millis: 1402564173708]\
// HTTP/1.1 200 OK [OkHttp-Selected-Protocol: spdy/3.1, server: cloudflare-nginx, date: \
// Wed, 18 Jun 2014 10:35:44 GMT, content-type: text/html; charset=UTF-8, set-cookie: \
// __cfduid=dec44ce8ac515d613f7490568f39612f21403087744468; expires=Mon, 23-Dec-2019 23:50:00 GMT; \
// path=/; domain=.cloudflare.com; HttpOnly, expires: Wed, 18 Jun 2014 14:35:44 GMT, cache-control: \
// public, max-age=14400, pragma: no-cache, x-frame-options: DENY, vary: Accept-Encoding, \
// strict-transport-security: max-age=31536000, cf-cache-status: HIT, cf-ray: 13c6d782e0ac0d31-LAX, \
// OkHttp-Sent-Millis: 1403087740701, OkHttp-Received-Millis: 1403087741183]
System.out.println(response);
String actual = EntityUtils.toString(response.getEntity(), UTF_8);
System.out.println(actual);
System.out.println(":::\n");
System.out.println("::: It's a Url connection alike api usage.");
// ::: It's a Url connection alike api usage
// firstly, we create a okhttp lib client for further using
OkHttpClient ok = new OkHttpClient();
ok.setHostnameVerifier(UTILS.createInsecureHostnameVerifier());
ok.setSslSocketFactory(UTILS.createInsecureSslSocketFactory());
// we config a host with a prefered portocol kind
// actally http://118.186.217.31 hosts with a spdy style http service
// ok.configPreferredHostProtocol("118.186.217.31", Protocol.SPDY_3);
// with the customized client, create a url factory
OkUrlFactory factory = new OkUrlFactory(ok);
// open an connection from a URL
HttpURLConnection connection = factory.open(new URL("https://118.186.217.31"));
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF_8));
for (String line = null; (line = reader.readLine()) != null;) System.out.println(line);
reader.close();
connection.disconnect();
System.out.println(":::\n");
}