写了一下午,借鉴apache的 httpclient 源码 调用 写的,拿出来分享一下,可以用作其他不同平台的项目post/get数据上面。

package cn.shb.test;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set; /**
* Created by caozengling on 2016/9/27.
*/
public class ZhuanLiSearch {
//超时间隔
private static int connectTimeOut = 60000;
//让connectionmanager管理httpclientconnection时是否关闭连接
public static boolean alwaysClose = false;
//返回数据编码格式
private String encoding = "UTF-8"; private final HttpClient client = new HttpClient(new SimpleHttpConnectionManager()); public HttpClient getHttpClient() {
return client;
} /**
* 用法:
* HttpRequestProxy hrp = new HttpRequestProxy();
* hrp.doRequest("http://www.163.com",null,null,"gbk");
*
* @param url 请求的资源URL
* @param postData POST请求时form表单封装的数据 没有时传null
* @param header request请求时附带的头信息(header) 没有时传null
* @param encoding response返回的信息编码格式 没有时传null
* @return response返回的文本数据
* @throws //CustomException
*/
public String doRequest(String url, Map postData, Map header, String encoding) throws Exception {
String responseString = null;
//头部请求信息
Header[] headers = null;
if (header != null) {
Set entrySet = header.entrySet();
int dataLength = entrySet.size();
headers = new Header[dataLength];
int i = 0;
for (Iterator itor = entrySet.iterator(); itor.hasNext(); ) {
Map.Entry entry = (Map.Entry) itor.next();
headers[i++] = new Header(entry.getKey().toString(), entry.getValue().toString());
}
}
//post方式
if (postData != null) {
PostMethod postRequest = new PostMethod(url.trim());
if (headers != null) {
for (int i = 0; i < headers.length; i++) {
postRequest.setRequestHeader(headers[i]);
}
}
Set entrySet = postData.entrySet();
int dataLength = entrySet.size();
NameValuePair[] params = new NameValuePair[dataLength];
int i = 0;
for (Iterator itor = entrySet.iterator(); itor.hasNext(); ) {
Map.Entry entry = (Map.Entry) itor.next();
params[i++] = new NameValuePair(entry.getKey().toString(), entry.getValue().toString());
}
postRequest.setRequestBody(params);
try {
responseString = this.executeMethod(postRequest, encoding);
} catch (Exception e) { } finally {
postRequest.releaseConnection();
}
}
//get方式
if (postData == null) {
GetMethod getRequest = new GetMethod(url.trim());
if (headers != null) {
for (int i = 0; i < headers.length; i++) {
getRequest.setRequestHeader(headers[i]);
}
}
try {
responseString = this.executeMethod(getRequest, encoding);
} catch (Exception e) { } finally {
getRequest.releaseConnection();
}
} return responseString;
} private String executeMethod(HttpMethod request, String encoding) throws Exception {
String responseContent = null;
InputStream responseStream = null;
BufferedReader rd = null;
try {
this.getHttpClient().executeMethod(request);
if (encoding != null) {
responseStream = request.getResponseBodyAsStream();
rd = new BufferedReader(new InputStreamReader(responseStream,
encoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null) {
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
} else
responseContent = request.getResponseBodyAsString(); Header locationHeader = request.getResponseHeader("location");
//返回代码为302,301时,表示页面己经重定向,则重新请求location的url,这在
//一些登录授权取cookie时很重要
if (locationHeader != null) {
String redirectUrl = locationHeader.getValue();
this.doRequest(redirectUrl, null, null, null);
}
} catch (HttpException e) { } catch (IOException e) { } finally {
if (rd != null)
try {
rd.close();
} catch (IOException e) { }
if (responseStream != null)
try {
responseStream.close();
} catch (IOException e) { }
}
return responseContent;
} /**
* 特殊请求数据,这样的请求往往会出现redirect本身而出现递归死循环重定向
* 所以单独写成一个请求方法
* 比如现在请求的url为:http://localhost:8080/demo/index.jsp
* 返回代码为302 头部信息中location值为:http://localhost:8083/demo/index.jsp
* 这时httpclient认为进入递归死循环重定向,抛出CircularRedirectException异常
*
* @param url
* @return
* @throws //CustomException
*/
public String doSpecialRequest(String url, int count, String encoding) throws Exception {
String str = null;
InputStream responseStream = null;
BufferedReader rd = null;
GetMethod getRequest = new GetMethod(url);
//关闭httpclient自动重定向动能
getRequest.setFollowRedirects(false);
try { this.client.executeMethod(getRequest);
Header header = getRequest.getResponseHeader("location");
if (header != null) {
//请求重定向后的URL,count同时加1
this.doSpecialRequest(header.getValue(), count + 1, encoding);
}
//这里用count作为标志位,当count为0时才返回请求的URL文本,
//这样就可以忽略所有的递归重定向时返回文本流操作,提高性能
if (count == 0) {
getRequest = new GetMethod(url);
getRequest.setFollowRedirects(false);
this.client.executeMethod(getRequest);
responseStream = getRequest.getResponseBodyAsStream();
rd = new BufferedReader(new InputStreamReader(responseStream,
encoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null) {
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
str = tempStr.toString();
} } catch (HttpException e) { } catch (IOException e) { } finally {
getRequest.releaseConnection();
if (rd != null)
try {
rd.close();
} catch (IOException e) { }
if (responseStream != null)
try {
responseStream.close();
} catch (IOException e) { }
}
return str;
} public static void main(String[] args) throws Exception {
ZhuanLiSearch hrp = new ZhuanLiSearch ();
Map header = new HashMap();
header.put("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)"); Map params = new HashMap(); params.put("wd", "zhang");
StringBuffer sb = new StringBuffer();
sb.append("23200820137723.6");
String str = hrp.doRequest(
"http://www.soopat.com/Home/Result?SearchWord=" +
sb,
null, header, null);
System.out.println(str);
if (str.indexOf("NotFoundContent")!=-1){
System.out.println("1");
}else {
System.out.println("2");
}
// System.out.println(str.contains("</title>"));
// System.out.println(str);
} }

  

最新文章

  1. Spring之旅(2)
  2. Erlang中如何在同一台机器上运行多个erlang节点?
  3. SQL--存储过程
  4. Java 嵌套解析 json
  5. Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别
  6. hdu 3018 Ant Trip 欧拉回路+并查集
  7. mysql 1449 : The user specified as a definer (&#39;root&#39;@&#39;%&#39;) does not exist 解决方法
  8. php中检查文件或目录是否存在的代码小结
  9. Eclipse 中引用其他项目及项目打包
  10. 【学习笔记】【Foundation】数组
  11. 自己写CPU第五级(5)——测试逻辑、实现移动和空指令
  12. 解读Java内部类
  13. iOS界面不能点击(tableView 的cell 不能使用点击事件,tableView也不能上下滚动)
  14. 内核ring buffer -- kfifo
  15. ftp和mysql数据库结合使用
  16. java - 并发编程易错实例
  17. python中字符串连接的四种方式
  18. September 27th 2017 Week 39th Wednesday
  19. 重写lucene.net的分词器支持3.0.3.0版本
  20. Linux下编译安装MySQL5.6

热门文章

  1. win7中VS2010中安装CSS3.0问题解决方法
  2. linux 安全狗
  3. POJ2104 K-TH NUMBER 传说中的主席树
  4. jquery中attr()与prop()区别
  5. C#多线程同步事件及等待句柄AutoResetEvent 和 ManualResetEvent
  6. Matlab2015矩阵表示03
  7. Android基础总结(一)
  8. 解决:Cannot get http://gerrit.googlesource.com/git-repo/clone.bundle
  9. get last dirname/filename in a file path argument
  10. JAVA之IO流(字节流)