java 伪装成浏览器向 网站发起请求
来源:互联网 发布:ubuntu 启动 黑屏 u盘 编辑:程序博客网 时间:2024/06/05 15:38
package MgUtil.Util;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.xwork.StringUtils;
/**
/**
* @author 鲁炬
*
*/
public class HttpClientUtil {
public static HttpClient getClient() {
HttpClient client = new HttpClient();
return client;
}
public static String getHtml(String url) throws HttpException, IOException {
return getHtml(url, 80, null, null, 0, null);
}
public static String getHtml(String url, String cookie) throws HttpException, IOException {
return getHtml(url, 80, null, null, 0, cookie);
}
public static String getHtml(String url, int port, String cookie) throws HttpException, IOException {
return getHtml(url, port, null, null, 0, cookie);
}
public static String getHtml(String url, int port, String encoding, String proxyHost, int proxyPort, String cookie)
throws HttpException, IOException {
HttpClient httpClient = getClient();
String rest = null;
if(proxyHost != null && proxyPort != 0) httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
HttpMethod method = new GetMethod(url);
if(!StringUtils.isBlank(cookie)) {
method.addRequestHeader("Cookie", cookie);
}
method.addRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727");
//Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
httpClient.executeMethod(method);
//根据http头解析正确的字符集
String header = method.getResponseHeader("Content-Type").getValue();
if(header.contains("charset=")) {
encoding = header.substring(header.indexOf("charset=") + "charset=".length(), header.length());
}
if(encoding == null) encoding = "GBK";
rest = new String(method.getResponseBody(), encoding);
method.releaseConnection();
return rest;
}
public static void main(String[] args) throws HttpException, IOException {
String url = "http://blog.csdn.net/xmlrequest/article/details/11519503";
System.out.println(getHtml(url));
}
}
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.xwork.StringUtils;
/**
* java 伪装成浏览器向网站发起请求
*需要 commons.httpclient.jar和 commons-logging.jar
*//**
* @author 鲁炬
*
*/
public class HttpClientUtil {
public static HttpClient getClient() {
HttpClient client = new HttpClient();
return client;
}
public static String getHtml(String url) throws HttpException, IOException {
return getHtml(url, 80, null, null, 0, null);
}
public static String getHtml(String url, String cookie) throws HttpException, IOException {
return getHtml(url, 80, null, null, 0, cookie);
}
public static String getHtml(String url, int port, String cookie) throws HttpException, IOException {
return getHtml(url, port, null, null, 0, cookie);
}
public static String getHtml(String url, int port, String encoding, String proxyHost, int proxyPort, String cookie)
throws HttpException, IOException {
HttpClient httpClient = getClient();
String rest = null;
if(proxyHost != null && proxyPort != 0) httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
HttpMethod method = new GetMethod(url);
if(!StringUtils.isBlank(cookie)) {
method.addRequestHeader("Cookie", cookie);
}
method.addRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727");
//Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
httpClient.executeMethod(method);
//根据http头解析正确的字符集
String header = method.getResponseHeader("Content-Type").getValue();
if(header.contains("charset=")) {
encoding = header.substring(header.indexOf("charset=") + "charset=".length(), header.length());
}
if(encoding == null) encoding = "GBK";
rest = new String(method.getResponseBody(), encoding);
method.releaseConnection();
return rest;
}
public static void main(String[] args) throws HttpException, IOException {
String url = "http://blog.csdn.net/xmlrequest/article/details/11519503";
System.out.println(getHtml(url));
}
}
- java 伪装成浏览器向 网站发起请求
- 客户端浏览器向服务器发起http请求的全过程
- Java HttpClient 如何伪装微信浏览器进行POST请求
- java 发起HTTP请求
- Java发起Http请求
- Java发起http 请求
- java 发起http请求
- Python伪装浏览器请求爬虫豆瓣小组
- Android 向任意网址发起数据请求
- 用javascript模拟用户发起浏览器请求
- 浏览器发起网络请求及优化
- java后台发起请求方式
- 爬虫伪装成浏览器访问
- WEB请求处理(1):浏览器请求发起处理
- Python向PHP发起GET与POST请求
- iOS uiwebview向.net后端发起请求无效的问题
- java发起http请求提交表单
- java发起HttpURLConnection和HttpsURLConnection请求
- 黑马程序员_java面向对象(二) 继承
- Android屏幕解锁图案破解
- ClassLoader获取的两种方式
- 化解提笔忘字依赖技术手段——汉字拼形
- 【热门主题: 高清雪景桌面壁纸】
- java 伪装成浏览器向 网站发起请求
- linux 定时备份数据资料
- android 中 系统日期时间的获取
- 函数指针高阶运用(C++函数指针)
- SQL面试题大全
- <iOS>other linker flags
- 友盟SDK统计数据
- DG 一次大意操作总结:
- Block 的内存管理