HttpClient的Post和Get訪问网页
一、基础JAR包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
二、源码
package com.qidou.httpclient; import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger; /**
* HTTP请求客户端。提供GET和POST两种方式发送请求。
*
* @author Administrator
*
*/
public class HttpClient
{
/**
* 日志对象。 */
private static Logger logger = Logger.getLogger(HttpClient.class); /**
* 默认HTTP请求客户端对象。
*/
private DefaultHttpClient _httpclient; /**
* 用户自己定义消息头。 */
private Map<String, String> _headers; /**
* 使用默认客户端对象。 */
public HttpClient()
{
// 1. 创建HttpClient对象。 _httpclient = new DefaultHttpClient();
logger.info("create _httpclient ...");
} /**
* 调用者指定客户端对象。
*
* @param httpclient
*/
public HttpClient(Map<String, String> headers)
{
// 1. 创建HttpClient对象。 _httpclient = new DefaultHttpClient();
this._headers = headers;
logger.info("create _httpclient ...");
} /**
* HTTP POST请求。
*
* @param url
* @param params
* @return
* @throws InterruptedException
*/
public HttpResponse post(String url, Map<String, String> params)
{
// 2. 创建请求方法的实例,并指定请求URL,加入请求參数。
HttpPost post = postForm(url, params);
logger.info("create httppost : " + url); return invoke(post);
} /**
* HTTP GET请求。
*
* @param url
* @return
*/
public HttpResponse get(String url)
{
HttpGet get = new HttpGet(url);
logger.info("create httpget : " + url); return invoke(get);
} /**
* 发送请求,处理响应。
* @param request
* @return
*/
private HttpResponse invoke(HttpUriRequest request)
{
if (this._headers != null)
{
//
addHeaders(request);
logger.info("addHeaders to http ...");
} HttpResponse response = null;
try
{
// 3. 调用HttpClient对象的execute(HttpUriRequest request)发送请求。返回一个HttpResponse。
response = _httpclient.execute(request);
logger.info("execute http success... ; body = " + EntityUtils.toString(response.getEntity()));
}
catch (Exception e)
{
e.printStackTrace();
logger.info("execute http exception...");
}
finally
{
// 4. 不管运行方法是否成功。都必须释放连接。 request.abort();
logger.info("release http ...");
} return response;
} /**
* 获取post方法。
*
* @param url
* @param params
* @return
*/
private HttpPost postForm(String url, Map<String, String> params)
{
HttpPost httpost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); // 组装參数。 Set<String> keySet = params.keySet();
for (String key : keySet)
{
nvps.add(new BasicNameValuePair(key, params.get(key)));
} try
{
logger.info("set utf-8 form entity to httppost ...");
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} return httpost;
} /**
* 添加消息头。
*
* @param httpost
*/
private void addHeaders(HttpUriRequest httpost)
{
Iterator<Entry<String, String>> it = this._headers.entrySet()
.iterator();
Entry<String, String> entry = null;
String name = null;
String value = null; while (it.hasNext())
{
entry = it.next();
name = entry.getKey();
value = entry.getValue(); httpost.addHeader(name, value);
}
} /**
* 关闭HTTP客户端链接。
*/
public void shutdown()
{
_httpclient.getConnectionManager().shutdown();
logger.info("shutdown _httpclient ...");
}
}
三、測试演示样例
package com.qidou.httpclient; import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse; /**
* HTTPclient測试类。
*/
public class TestHttpClient
{
/**
* HTTP请求client。 */
private HttpClient httpClient; /**
* 初始化HTTP请求client。
*/
{
System.out.println("init http client..."); Map<String,String> headers = new HashMap<String,String>();
headers.put("Accept", "*/*");
headers.put("Accept-Encoding", "gzip, deflate");
headers.put("Accept-Language", "zh-cn");
headers.put("Connection", "Keep-Alive");
headers.put("Cookie", "UC_SID=66134277449dcc94b77dfbcdb65a3d70; cna=HnmMDIROzW4CAbdDI+iUySCj; PHPSESSID=91818085719e575f59bd5f4b31df402f; lzstat_uv=260674130547154807|3037573; lzstat_ss=582317835_2_1409680845_3037573; PHPSESSID=91818085719e575f59bd5f4b31df402f; frontvar=lns%3Dmenu-1%2Cflow-0%2Ctraf-0%2Ccont-0%2Cvisitor-0%2Ceanalysis-0%2Cbigdata-0%2C%26cmenu%3Dsite_overview%26siteShowHis%3Dopen; CNZZDATA30086426=cnzz_eid%3D2088392191-1409645088-http%253A%252F%252Ftongji.cnzz.com%252F%26ntime%3D1409650488; CNZZDATA30069868=cnzz_eid%3D241047449-1409644292-http%253A%252F%252Ftongji.cnzz.com%252F%26ntime%3D1409647196; CNZZDATA33222=cnzz_eid%3D1336832806-1409645823-http%253A%252F%252Ftongji.cnzz.com%252F%26ntime%3D1409651227; CNZZDATA30001831=cnzz_eid%3D732886644-1409644865-http%253A%252F%252Ftongji.cnzz.com%252F%26ntime%3D1409649918; _cnzz_CV30069868=%E6%98%AF%E5%90%A6%E7%99%BB%E5%BD%95%7C%E6%AD%A3%E5%B8%B8%E7%99%BB%E5%BD%95%7C1409680810339");
headers.put("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)");
headers.put("Referer", "http://www.baidu.com/");
headers.put("x-requested-with", "XMLHttpRequest"); //新建HTTP请求client。能够自己设置消息头的一些參数 : httpClient = new HttpClient(headers);
httpClient = new HttpClient();
} /**
* 測试方法。
* 实现过程:
* 1、使用username/密码登陆人人网 : http://www.renren.com/PLogin.do
* 2、訪问博客 : http://blog.renren.com/blog/304317577/449470467
*
*/
@org.junit.Test
public void testPost()
{
String url = "http://www.renren.com/PLogin.do";
String redirectURL = "http://blog.renren.com/blog/304317577/449470467";
String userName = "。。。 。";
String password = "。。。 。"; /**
* 登陆參数。 */
Map<String,String> params = new HashMap<String,String>();
params.put("origURL", redirectURL);
params.put("domain", "renren.com");
params.put("isplogin", "true");
params.put("formName", "");
params.put("method", "");
params.put("submit", "登录");
params.put("email", userName);
params.put("password", password); //发送请求登陆人人网。
HttpResponse postResponse = httpClient.post(url,params); //获取消息头的跳转地址,跳转到指定博客。
String redirectLocation = postResponse.getFirstHeader("Location").getValue();
System.out.println(redirectLocation); //获取跳转博客网页。 httpClient.get(redirectLocation); //关闭HTTP请求client。
httpClient.shutdown();
}
}
四、执行结果
2014-09-04 18:31:48 [com.qidou.httpclient.HttpClient]-[INFO] create _httpclient ...
2014-09-04 18:31:48 [com.qidou.httpclient.HttpClient]-[INFO] set utf-8 form entity to httppost ...
2014-09-04 18:31:48 [com.qidou.httpclient.HttpClient]-[INFO] create httppost : http://www.renren.com/PLogin.do
2014-09-04 18:31:49 [org.apache.http.client.protocol.ResponseProcessCookies]-[WARN] Cookie rejected: "[version: 0][name: t][value: f28a837f39c7773213280e099b611aaf][domain: renren.com][path: /xtalk/][expiry: null]". Illegal path attribute "/xtalk/". Path of
origin: "/PLogin.do"
2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] execute http success... ; body = The URL has moved <a href="http://blog.renren.com/blog/304317577/449470467">here</a>
2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] release http ...
http://blog.renren.com/blog/304317577/449470467
2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] create httpget : http://blog.renren.com/blog/304317577/449470467
2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] execute http success... ; body = 博客网页太大略去。。。
2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] release http ...
2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] shutdown _httpclient ...
HttpClient的Post和Get訪问网页的更多相关文章
- 基于HttpClient 4.3的可訪问自签名HTTPS网站的新版工具类
本文出处:http://blog.csdn.net/chaijunkun/article/details/40145685,转载请注明.因为本人不定期会整理相关博文,会对相应内容作出完好.因此强烈建议 ...
- Android通过HTTP POST带參訪问asp.net网页
在看了网络上非常多视频关于android通过HTTP POST或者GET方式訪问网页并获取数据的方法. 自己也copy了一份来測试.并通过C#.NET搭建了一个简单的后台,但发现传參时,依照网上的方式 ...
- 利用httpclient和多线程刷訪问量代码
缘起于玩唱吧,由于唱吧好友少,訪问量低,又不想加什么亲友团之类的,主要是太麻烦了,于是我就琢磨唱吧的訪问机制,准备用java的httpclient库来进行刷訪问量,想到动态IP反复使用就想到了用多线程 ...
- httpclient模拟浏览器訪问站点
HttpClient 是 Apache Jakarta Common 下的子项目.能够用来提供高效的.最新的.功能丰富的支持 HTTP 协议的client编程工具包.而且它支持 HTTP 协议最新的版 ...
- 訪问的网页自己主动打开QQ对话
今天訪问中国论文网,自己主动就弹出与QQ的对话框,非常是好奇.于是查看网页源码发现例如以下可疑处: <script type="text/javascript"> fu ...
- Android訪问网络,使用HttpURLConnection还是HttpClient?
原文地址:http://android-developers.blogspot.com/2011/09/androids-http-clients.html 大多数的Android应用程序都会使用HT ...
- Android网络编程之使用HTTP訪问网络资源
使用HTTP訪问网络资源 前面介绍了 URLConnection己经能够很方便地与指定网站交换信息,URLConnection另一个子类:HttpURLConnection,HttpURLConnec ...
- SQL Server未找到或无法訪问server问题解决
问题信息:"在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法訪问服务器.请验证实例名称是否正确而且 SQL Server 已配置为同意远程连接. (pr ...
- java后台訪问url连接——HttpClients
java后台訪问url,并传递数据--通过httpclient方式 须要的包,包可能多几个额外的,假设无用或者冲突删除就可以.httpclient是使用的是4.4.1的版本号:http://downl ...
随机推荐
- golang 并发锁的陷阱
错误代码示例 package main import ( "sync" "strconv" "fmt" ) type Node struct ...
- 解决 HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException:
HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException: The foll ...
- (转)Hibernate框架基础——Java对象持久化概述
http://blog.csdn.net/yerenyuan_pku/article/details/52732990 Java对象持久化概述 应用程序的分层体系结构 基于B/S的典型三层架构 说明 ...
- Call stack Structure
The stack frame at the top of the stack is for the currently executing routine. Th ...
- 05C语言数组
C语言数组 一维数组 类型符 数组名[常量表达式] #include <stdio.h> int main(){ ] = {,,,}; int a; ;a<;a++){ printf ...
- Microsoft SQL Server 存储过程
Microsoft SQL Server 存储过程 TRIGGER DDL触发器:主要用于防止对数据库架构.视图.表.存储过程等进行的某些修改:DDL事件是指对数据库CREATE,ALTER,DROP ...
- __cdecl、__stdcall、__fastcall 与 __pascal 浅析
call 指令与 retn 指令 首先我们得了解 CALL 和 RETN 指令的作用,才能更好地理解调用规则,这也是先决条件. 实际上,CALL 指令就是先将下一条指令的 EIP 压栈,然后 JMP ...
- mongodb--命令练习
windows10安装下载mongodb # 官网或镜像地址下载mongodb exe 二进制安装包 # 安装时选用custorm 去除勾选compass可视化工具 # 安装完成创建data文件,修改 ...
- MessageFormat理解,MessageFormat.format(Object obj)方法
MessageFormat.format(Object obj)方法主要用途为拼接message信息 用法: Object[] testArgs = {new String("张三" ...
- hadoop 3.0.0新特性
1.Minimum required Java version increased from Java 7 to Java 8 java最低支持版本变成java8 2.Support for eras ...