设置代理

来源:互联网 发布:软件测试结论 编辑:程序博客网 时间:2022/10/05 06:49
package com.cyyun.post;import java.io.BufferedInputStream;import java.io.IOException;import java.net.URL;import java.net.URLConnection;import org.apache.commons.io.IOUtils;import org.apache.log4j.Logger;/** * @author Jason * @date Oct 27, 2010 * @version 1.0 */public class TestProxyIp {private static final Logger log = Logger.getLogger(TestProxyIp.class);public static void main(String[] args) throws IOException {System.setProperty("http.maxRedirects", "50");System.getProperties().setProperty("proxySet", "true");// 如果不设置,只要代理IP和代理端口正确,此项不设置也可以String ip = "93.91.200.146";ip = "221.130.18.5";ip = "221.130.23.135";ip = "221.130.18.78";ip = "221.130.23.134";ip = "221.130.18.49";ip = "111.1.32.36";ip = "221.130.18.49";ip = "221.130.18.49";//System.getProperties().setProperty("http.proxyHost", ip);//System.getProperties().setProperty("http.proxyPort", "80");// 确定代理是否设置成功log.info(getHtml("http://www.ip.cn/"));}private static String getHtml(String address) {StringBuffer html = new StringBuffer();String result = null;try {URL url = new URL(address);URLConnection conn = url.openConnection();conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");BufferedInputStream in = new BufferedInputStream(conn.getInputStream());     String str = IOUtils.toString(in,"utf-8");     System.out.println(str);} catch (Exception e) {e.printStackTrace();return null;} finally {html = null;}return result;}}

 

package com.cyyun.post;import java.io.IOException;import java.net.MalformedURLException;import com.gargoylesoftware.htmlunit.BrowserVersion;import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;import com.gargoylesoftware.htmlunit.WebClient;import com.gargoylesoftware.htmlunit.html.HtmlPage;public class ProxyHtmlunit {public static void main(String args[]) throws FailingHttpStatusCodeException, MalformedURLException, IOException {final WebClient// webClient = new WebClient(BrowserVersion.FIREFOX_24,"123.131.176.87",8585);webClient = new WebClient(BrowserVersion.FIREFOX_24,"124.72.74.252",8088);// webClient = new WebClient(BrowserVersion.FIREFOX_24); webClient.waitForBackgroundJavaScript(4000);final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();//credentialsProvider.addCredentials("username","password");//116.226.68.174 HtmlPage p =(HtmlPage) webClient.getPage("http://www.ip.cn/");System.out.println(p.getTitleText());System.out.println(p.asText());}}

 

 

0 0
原创粉丝点击