我正在尝试使用 JSoup 来抓取临时服务器上的一些页面。要使用浏览器查看登台服务器上的页面,我需要连接到 VPN。
我已连接到 VPN,但当我使用 JSoup 尝试抓取页面时,它总是超时。如何让我的程序使用 VPN 连接。还是还有其他我没有想到的事情?
注意:我还在程序的另一部分中使用了 HttpClient。有没有一种方法可以将我的程序设置为在程序初始化后连接到 VPN/代理,以便 JSoup 和 HttpClient 都使用 VPN/代理。
Thanks
您可以为代理设置 java 属性:
// if you use https, set it here too
System.setProperty("http.proxyHost", "<proxyip>"); // set proxy server
System.setProperty("http.proxyPort", "<proxyport>"); // set proxy port
Document doc = Jsoup.connect("http://your.url.here").get(); // Jsoup now connects via proxy
或者将网站下载到字符串中并解析它:
final URL website = new URL("http://your.url.here"); // The website you want to connect
// -- Setup connection through proxy
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("<proxyserver>", 1234)); // set proxy server and port
HttpURLConnection httpUrlConnetion = (HttpURLConnection) website.openConnection(proxy);
httpUrlConnetion.connect();
// -- Download the website into a buffer
BufferedReader br = new BufferedReader(new InputStreamReader(httpUrlConnetion.getInputStream()));
StringBuilder buffer = new StringBuilder();
String str;
while( (str = br.readLine()) != null )
{
buffer.append(str);
}
// -- Parse the buffer with Jsoup
Document doc = Jsoup.parse(buffer.toString());
您可以使用HttpClient
对于这个解决方案也是如此。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)