java程序访问网页,需要的代理设置

这篇博客提供了三种使用Java访问网页时设置代理的方法,包括HttpClient、WebClient和Jsoup。示例代码详细展示了如何配置代理主机和端口,以及在请求中设置额外的参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
}

我只想到这几个,如果有别的,请告诉我,我们一起努力。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值