java程序访问网页,需要的代理设置
2015-02-11 13:16
561 查看
1、通过HttpClient获取网页信息(发送信息到网页)
public static void setDuanxin(){
HttpClient client = new HttpClient();
client.getHostConfiguration().setProxy("192.168.0.1",8080);//设置代理
// client.getHostConfiguration().setHost( "sms.webchinese.cn" , 8080, "http" );
PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
post.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
NameValuePair[] data = { new NameValuePair("Uid", "安徽*****"), // 注册的用户名
new NameValuePair("Key", "****************"), // 注册成功后,登录网站使用的密钥
new NameValuePair("smsMob", "181********"), // 手机号码
new NameValuePair("smsText", "你好,你的猪已经送到。请查收") };//设置短信内容
post.setRequestBody(data);
try {
client.executeMethod(post);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
System.out.println("statusCode:" + statusCode);
for (Header h : headers) {
System.out.println(h.toString());
}
String result="";
try {
result = new String(post.getResponseBodyAsString().getBytes("gbk"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("结果:\t"+result);
post.releaseConnection();
}
2、通过WebClient获取网页信息
private static String getHtml(String url) {
ProxyConfig proxy =new ProxyConfig();
proxy.setProxyHost("192.168.0.1");
proxy.setProxyPort(8080);
String urlString = url;
if (!url.startsWith("http://")) {
urlString = "http://" + url;
}
WebClient webClient;
String html = null;
webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setProxyConfig(proxy);
webClient.setJavaScriptEnabled(false);
webClient.setCssEnabled(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.setTimeout(60000);
webClient.setThrowExceptionOnScriptError(false);
HtmlPage page;
try {
//会有类型转换错误异常
page = (HtmlPage) webClient.getPage(urlString);
html = page.asXml();
} catch (Exception e) {
System.out.println("此url不合法" + url+e);
}
return html;
}
3、通过jsoup获取网页信息
public static Document getHtmlDoc(String url) {
if (url == null || url.isEmpty())
return null;
try {
System.getProperties().put("http.proxySet", "true");
System.getProperties().put("http.proxyHost", "192.168.0.75");
System.getProperties().put("http.proxyPort", "808");
// System.getProperties().put("http.proxyUser", user);
// System.getProperties().put("http.proxyPassword", password);
System.getProperties().put("http.nonProxyHosts", "localhost|127.0.0.1");
return Jsoup.connect(url).data("query", "Java")
.userAgent("Mozilla").cookie("auth", "token").get();
} catch (Exception e) {
return null;
}
}
我只想到这几个,如果有别的,请告诉我,我们一起努力。
public static void setDuanxin(){
HttpClient client = new HttpClient();
client.getHostConfiguration().setProxy("192.168.0.1",8080);//设置代理
// client.getHostConfiguration().setHost( "sms.webchinese.cn" , 8080, "http" );
PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
post.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
NameValuePair[] data = { new NameValuePair("Uid", "安徽*****"), // 注册的用户名
new NameValuePair("Key", "****************"), // 注册成功后,登录网站使用的密钥
new NameValuePair("smsMob", "181********"), // 手机号码
new NameValuePair("smsText", "你好,你的猪已经送到。请查收") };//设置短信内容
post.setRequestBody(data);
try {
client.executeMethod(post);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
System.out.println("statusCode:" + statusCode);
for (Header h : headers) {
System.out.println(h.toString());
}
String result="";
try {
result = new String(post.getResponseBodyAsString().getBytes("gbk"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("结果:\t"+result);
post.releaseConnection();
}
2、通过WebClient获取网页信息
private static String getHtml(String url) {
ProxyConfig proxy =new ProxyConfig();
proxy.setProxyHost("192.168.0.1");
proxy.setProxyPort(8080);
String urlString = url;
if (!url.startsWith("http://")) {
urlString = "http://" + url;
}
WebClient webClient;
String html = null;
webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setProxyConfig(proxy);
webClient.setJavaScriptEnabled(false);
webClient.setCssEnabled(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.setTimeout(60000);
webClient.setThrowExceptionOnScriptError(false);
HtmlPage page;
try {
//会有类型转换错误异常
page = (HtmlPage) webClient.getPage(urlString);
html = page.asXml();
} catch (Exception e) {
System.out.println("此url不合法" + url+e);
}
return html;
}
3、通过jsoup获取网页信息
public static Document getHtmlDoc(String url) {
if (url == null || url.isEmpty())
return null;
try {
System.getProperties().put("http.proxySet", "true");
System.getProperties().put("http.proxyHost", "192.168.0.75");
System.getProperties().put("http.proxyPort", "808");
// System.getProperties().put("http.proxyUser", user);
// System.getProperties().put("http.proxyPassword", password);
System.getProperties().put("http.nonProxyHosts", "localhost|127.0.0.1");
return Jsoup.connect(url).data("query", "Java")
.userAgent("Mozilla").cookie("auth", "token").get();
} catch (Exception e) {
return null;
}
}
我只想到这几个,如果有别的,请告诉我,我们一起努力。
相关文章推荐
- Tomcat中设置Java 客户端程序的http(https)访问代理
- java设置代理访问URL
- java 设置代理访问http
- 在java程序中设置http代理访问网络
- java servlet 几种页面跳转的方法,需要的朋友可以参考一下 在访问网页的过程中,页面自动刷新、跳转和重定向是经常用到的,这里就说说在Java中是如何实现这些功能的! 一、页面自动刷新
- HttpComponents(Apache HttpComponents Client 4.1.3)通过代理访问网页的设置方法
- 【解决】WordPress FTP连接服务器时出错,请检查设置,WordPress需要访问您网页服务器的权限
- Python 设置 IP 代理 访问网页 ( 用户名密码验证代理 )
- Python2设置用户代理User Agent访问网页
- 在Java程序中设置代理
- Java如何设置代理访问链接地址
- java设置代理访问URL
- 编写Python程序访问需要cookie的网页
- 首先需要安装JDK 6 sudo apt-get install sun-java6-jdk 之后需要设置默认的java程序 sudo update-alternatives --config java 按照提示输入对应的选项,指定为JD
- 在Java程序中设置代理
- 网页上运行java小程序时出现“应用程序已被安全设置阻止”的解决方法
- crawler_java应用集锦9:httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件_设置代理