HTTP+SPDY 客户端Java开发包:okhttp
okhttp 是一个 Java 的 HTTP+SPDY 客户端开发包,同时也支持 Android。
kHttp is an HTTP client that’s efficient by default:
- SPDY support allows all requests to the same host to share a socket.
- Connection pooling reduces request latency (if SPDY isn’t available).
- Transparent GZIP shrinks download sizes.
- Response caching avoids the network completely for repeat requests.
示例代码:
OkHttpClient client = new OkHttpClient(); String post(URL url, byte[] body) throws IOException { HttpURLConnection connection = client.open(url); OutputStream out = null; InputStream in = null; try { // Write the request. connection.setRequestMethod("POST"); out = connection.getOutputStream(); out.write(body); out.close(); // Read the response. if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new IOException("Unexpected HTTP response: " + connection.getResponseCode() + " " + connection.getResponseMessage()); } in = connection.getInputStream(); return readFirstLine(in); } finally { // Clean up. if (out != null) out.close(); if (in != null) in.close(); } }
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!