HTTP+SPDY 客户端Java开发包:okhttp

jopen 11年前

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();        }      }

项目主页:http://www.open-open.com/lib/view/home/1383139543921