HTTPClient

httpclient-4.0.1.jar
httpcore-4.0.1.jar
httpmime-4.0.1.jar- 又依赖于 mime4j(apache-mime4j-0.6.jar)
commons-codec-1.4.jar
commons-logging-1.1.1.jar

commons-io-1.4.jar – 为了更方便处理与 IO 有关的需求


2.1  最简单的获取网页内容的示例

try {
String urlString = "你的链接";
URL url = new URL(urlString); //代表了一个网址
InputStream is = url.openStream(); //获得网页的内容
//将InputStream转换为Reader,并使用缓冲读取,提高效率,同时可以按行读取内容
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-
8"));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}


2.2  URLConnection 的基本用法,设置代理

public void testFetch02(){
try {
String urlString ="你的请求地址";
URL url = new URL(urlString); //代表了一个网址
//首先创建HTTP代理,指定代理的地址和端口
Proxy proxy = new Proxy(Proxy.Type.HTTP,new
InetSocketAddress("79.120.193.53",80));
/**
* 首先打开一个连接对象
* 可以通过这个对象,在真正发起请求之前,设置一些其它的信息
* 比如:代理服务器等
*/
URLConnection conn = url.openConnection(proxy);
InputStream is = conn.getInputStream(); //获得网页的内容
//将InputStream转换为Reader,并使用缓冲读取,提高效率,同时可以按行
读取内容
BufferedReader br = new BufferedReader(new
InputStreamReader(is,"UTF-8"));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}

2.3  HttpURLConnection 的用法HttpURLConnection 是 URLConnection 的子类,它提供了更多与 HTTP 有关的处理方法、

try {
String urlString =
"http://localhost:8080/cms/backend/main.jsp";
URL url = new URL(urlString); //代表了一个网址
//设置是否自动进行重定向,缺省这个值为true
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection conn =
(HttpURLConnection)url.openConnection();
//设置HTTP METHOD
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
System.out.println("服务器响应代码为:"+code);
InputStream is = conn.getInputStream();
//将InputStream转换为Reader,并使用缓冲读取,提高效率,同时可以按行
读取内容
BufferedReader br = new BufferedReader(new
InputStreamReader(is,"UTF-8"));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}

3.  使用 HttpClient 获取网页内容

3.1使用GET方式向后台递交请求

public void testFetch01(){
try {
//HttpClient主要负责执行请求
HttpClient httpclient = new DefaultHttpClient();
//利用HTTP GET向服务器发起请求
HttpGet get = new HttpGet("http://localhost:8080/cms");
//获得服务器响应的的所有信息
HttpResponse response = httpclient.execute(get);
//获得服务器响应回来的消息体(不包括HTTP HEAD)
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream is = entity.getContent();
//将InputStream转换为Reader,并使用缓冲读取,提高效率,同时
可以按行读取内容
BufferedReader br = new BufferedReader(new
InputStreamReader(is,"UTF-8"));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
}
//释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放
httpclient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

3.2自动获得响应的编码信息

public void testFetch02(){
try {
//HttpClient主要负责执行请求
HttpClient httpclient = new DefaultHttpClient();
//利用HTTP GET向服务器发起请求
HttpGet get = new HttpGet("http://www.baidu.com/");//new
HttpGet("http://localhost:8080/cms");
//获得服务器响应的的所有信息
HttpResponse response = httpclient.execute(get);
//获得服务器响应回来的消息体(不包括HTTP HEAD)
HttpEntity entity = response.getEntity();
if(entity != null){
//获得响应的字符集编码信息
//即获取HTTP HEAD的:Content-
Type:text/html;charset=UTF-8中的字符集信息
String charset =
EntityUtils.getContentCharSet(entity);
System.out.println("响应的字符集是:"+charset);
InputStream is = entity.getContent();
//使用响应中的编码来解释响应的内容
BufferedReader br = new BufferedReader(new
InputStreamReader(is,charset));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
}
//释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放
httpclient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

3.3设置代理服务器,访问网站

public void testFetch03(){
try {
//HttpClient主要负责执行请求
HttpClient httpclient = new DefaultHttpClient();
//设置代理服务器
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new
HttpHost("121.12.249.207",3128));
//利用HTTP GET向服务器发起请求
HttpGet get = new HttpGet("http://www.baidu.com/");//new
HttpGet("http://localhost:8080/cms");
//获得服务器响应的的所有信息
HttpResponse response = httpclient.execute(get);
//获得服务器响应回来的消息体(不包括HTTP HEAD)
HttpEntity entity = response.getEntity();
if(entity != null){
//获得响应的字符集编码信息
//即获取HTTP HEAD的:Content-
Type:text/html;charset=UTF-8中的字符集信息
String charset =
EntityUtils.getContentCharSet(entity);
System.out.println("响应的字符集是:"+charset);
InputStream is = entity.getContent();
//使用响应中的编码来解释响应的内容
BufferedReader br = new BufferedReader(new
InputStreamReader(is,charset));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}

is.close();
}
//释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放
httpclient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


3.4获得重定向之后的网址信息

HttpClient 缺省情况下自动处理客户端重定向,即当你访问网页(比如 A 网页)之后,假设被重定向到了
B 网页,那么,HttpClient 将自动返回 B 网页的内容,无需再编程处理它!有时候我们可能想要知道 A 网
页被重定向到了哪里,也就是取得 B 网页的网址,那么可以通过下述例子获得:

public void testFetch04(){
try {
//HttpClient主要负责执行请求
HttpClient httpclient = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
//利用HTTP GET向服务器发起请求
HttpGet get = new
HttpGet("http://localhost:8080/cms/backend/main.jsp");
//获得服务器响应的的所有信息
HttpResponse response = httpclient.execute(get,context);
//获得重定向之后的主机地址信息
HttpHost targetHost =
(HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
System.out.println(targetHost); // http://localhost:8080
//获得实际的请求对象的URI(即重定向之后的
"/cms/backend/login.jsp")
HttpUriRequest actualRequest = (HttpUriRequest)
context.getAttribute(ExecutionContext.HTTP_REQUEST);
System.out.println(actualRequest.getURI());
//获得服务器响应回来的消息体(不包括HTTP HEAD)
HttpEntity entity = response.getEntity();
if(entity != null){
//获得响应的字符集编码信息
//即获取HTTP HEAD的:Content-
Type:text/html;charset=UTF-8中的字符集信息
String charset =
EntityUtils.getContentCharSet(entity);
System.out.println("响应的字符集是:"+charset);
InputStream is = entity.getContent();
//使用响应中的编码来解释响应的内容
BufferedReader br = new BufferedReader(new
InputStreamReader(is,charset));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
}
//释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放
httpclient.getConnectionManager().shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}

3.5自动Cookie处理

HttpClient 能够支持自动 Cookie 处理。设想一个典型的场景:首先打开登录页面,然后输入用户名和密
码登录,然后访问那些只有登录之后才能访问的网页……
如果我们用浏览器,因为浏览器可以将登录之后的会话信息用 Cookie 存储在本地,所以,登录之后的每次
请求,都会自动向服务器发送 Cookie 的信息,我们利用 HttpClient,这些过程都全部可以自动化处理
了。

public void testFetch05(){
try {
//HttpClient主要负责执行请求
HttpClient httpclient = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
//利用HTTP GET向服务器发起请求,
HttpGet get = new
HttpGet("http://localhost:8080/cms/backend/login.jsp");
//获得服务器响应的的所有信息
HttpResponse response = httpclient.execute(get,context);
//获得服务器响应回来的消息体(不包括HTTP HEAD)
HttpEntity entity = response.getEntity();
String charset = null;
if(entity != null){
//获得响应的字符集编码信息
//即获取HTTP HEAD的:Content-
Type:text/html;charset=UTF-8中的字符集信息
charset = EntityUtils.getContentCharSet(entity);
System.out.println("响应的字符集是:"+charset);
InputStream is = entity.getContent();
//使用响应中的编码来解释响应的内容
BufferedReader br = new BufferedReader(new
InputStreamReader(is,charset));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
}
//************* 执行登录请求 ********************//
HttpPost post = new
HttpPost("http://localhost:8080/cms/backend/LoginServlet");
//添加POST参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username","admin"));
nvps.add(new BasicNameValuePair("password","admin"));
post.setEntity(new UrlEncodedFormEntity(nvps,charset));
response = httpclient.execute(post);
entity = response.getEntity();
if(entity != null){
InputStream is = entity.getContent();
//使用响应中的编码来解释响应的内容
BufferedReader br = new BufferedReader(new
InputStreamReader(is,charset));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
}
get = new
HttpGet("http://localhost:8080/cms/backend/ArticleServlet");
response = httpclient.execute(get);
entity = response.getEntity();
if(entity != null){
InputStream is = entity.getContent();
//使用响应中的编码来解释响应的内容
BufferedReader br = new BufferedReader(new
InputStreamReader(is,charset));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
}
//释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放
httpclient.getConnectionManager().shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值