`
m635674608
  • 浏览: 5028783 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

设置代理

    博客分类:
  • java
 
阅读更多
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());
	}


	
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics