zhaohaolin 发表于 2013-1-26 12:50:12

(转)HttpClient基础

<div class="postbody">1.1      执行请求

HttpClient的最重要的功能是执行HTTP方法。一个HTTP方法的执行涉及到一个或多个HTTP请求或HTTP响应的交流,HttpClient通常是在内部处理的。用户将提供一个执行请求对象,HttpClient发送请求到目标服务器返回一个相应的响应对象,如果执行失败则抛出一个异常。所以,HttpClient API的主要切入点是HttpClient的接口,它定义了上述约定。
下面是一个请求执行过程中的最简单形式的例子:
<div style="line-height: 21px; background-color: #f3f5e9; font-size: 14px; border-left-color: #cccccc; padding: 4pt;">HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://localhost/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
    InputStream instream = entity.getContent();
    int l;
    byte[] tmp = new byte;
    while ((l = instream.read(tmp)) != -1) {
    }
}
页: [1]
查看完整版本: (转)HttpClient基础