您的位置:首页 > 编程语言 > Java开发

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;

}

}

我只想到这几个,如果有别的,请告诉我,我们一起努力。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: