HttpClient不是一个浏览器。它是一个客户端的 HTTP 通信实现库。HttpClient 的目标是发送和接收 HTTP 报文。 HttpClient 不会去缓存内容,执行嵌入在 HTML 页面中
获取返回信息的头数据:
的 javascript 代码,猜测内容类型,重新格式化请求/重定向 URI,或者其它和 HTTP运输无关的功能。
HttpClient最重要的功能是执行HTTP方法。一个HTTP方法的执行,包含一个或多个http请求/http响应。
简单的请求过程的实例:
package ch1;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
public class Test1 {
public static void main(String[] args) throws Exception {
//创建一个HttpClient;
HttpClient hc=HttpClients.createDefault();
//创建一个get请求
HttpGet httpGet=new HttpGet("http://www.baidu.com");
//执行请求,返回Response
HttpResponse httpResponse=hc.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
//获取内容
InputStream is=httpEntity.getContent();
byte[] b=new byte[1024];
int i;
StringBuffer sb=new StringBuffer();
while((i=is.read(b))!=-1){
sb.append(new String(b));
}
is.close();
System.out.println(sb.toString());
}
}
使用httpclient提供的方法创建带参数的uri:
package ch1;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class Test2 {
public static void main(String[] args) throws Exception {
//http://www.baidu.com/s?tn=baiduhome_pg&ie=utf-8&bs=httpclient+中文+pdf&f=8
//&rsv_bp=1&wd=csdn&rsv_sug3=5&rsv_sug4=229&rsv_sug1=6&rsv_sug2=0&inputT=4
HttpClient hc=HttpClients.createDefault();
//将请求的参数存在这个集合中
List<NameValuePair> nameValues=new ArrayList<NameValuePair>();
nameValues.add(new BasicNameValuePair("tn", "baiduhome_pg"));
nameValues.add(new BasicNameValuePair("ie", "utf-8"));
nameValues.add(new BasicNameValuePair("bs", "httpclient+中文+pdf"));
nameValues.add(new BasicNameValuePair("f", "8"));
nameValues.add(new BasicNameValuePair("rsv_bp", "1"));
nameValues.add(new BasicNameValuePair("wd", "csdn"));
nameValues.add(new BasicNameValuePair("rsv_sug3", "5"));
nameValues.add(new BasicNameValuePair("rsv_sug4", "299"));
nameValues.add(new BasicNameValuePair("rsv_sug1", "6"));
nameValues.add(new BasicNameValuePair("rsv_sug2", "0"));
nameValues.add(new BasicNameValuePair("inputT", "4"));
//创建一个URI
@SuppressWarnings("deprecation")
URI uri=URIUtils.createURI("http", "www.baidu.com", -1, "/s",URLEncodedUtils.format(nameValues, "utf-8") ,null);
System.out.println(uri);
HttpGet httpGet=new HttpGet(uri);
HttpResponse httpResponse=hc.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
//获取内容
InputStream is=httpEntity.getContent();
byte[] b=new byte[1024];
int i;
StringBuffer sb=new StringBuffer();
while((i=is.read(b))!=-1){
sb.append(new String(b));
}
is.close();
System.out.println(sb.toString());
}
}
Http响应:
package ch1;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.message.BasicHttpResponse;
public class Test3 {
public static void main(String[] args) {
//模拟创建一个HttpResponse
HttpResponse httpRes = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus.SC_OK,"OK");
System.out.println(httpRes.getProtocolVersion());
System.out.println(httpRes.getStatusLine());
}
}
获取返回信息的头数据:
//执行请求,返回Response
HttpResponse httpResponse=hc.execute(httpGet);
//获取状态
System.out.println(httpResponse.getStatusLine());
//获取头信息
System.out.println(httpResponse.getFirstHeader("Content-Type"));
Header[] h= httpResponse.getHeaders("Content-Type");
for (int i = 0; i < h.length; i++) {
System.out.println(h[i]);
}
//获取所有头信息
HeaderIterator hi=httpResponse.headerIterator();
while(hi.hasNext()){
Header he=hi.nextHeader();
System.out.println(he.getName()+":"+he.getValue());
}