Java 通过VPN/代理的JSoup

Java 通过VPN/代理的JSoup,java,proxy,httpclient,jsoup,vpn,Java,Proxy,Httpclient,Jsoup,Vpn,我正在尝试使用JSoup来刮取登台服务器上的一些页面。要使用浏览器查看登台服务器上的页面,我需要连接到VPN 我已连接到VPN,但当我使用JSoup尝试刮取页面时,它总是超时。如何使我的程序使用VPN连接。或者这里还有什么我没想到的 注意:我还在程序的另一部分中使用HttpClient。是否有一种方法可以在程序初始化后将程序设置为连接到VPN/代理,以便JSoup和HttpClient都使用VPN/代理 谢谢您可以为代理设置java属性: // if you use https, set it

我正在尝试使用JSoup来刮取登台服务器上的一些页面。要使用浏览器查看登台服务器上的页面,我需要连接到VPN

我已连接到VPN,但当我使用JSoup尝试刮取页面时,它总是超时。如何使我的程序使用VPN连接。或者这里还有什么我没想到的

注意:我还在程序的另一部分中使用HttpClient。是否有一种方法可以在程序初始化后将程序设置为连接到VPN/代理,以便JSoup和HttpClient都使用VPN/代理


谢谢

您可以为代理设置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
//如果使用https,也在此处设置
System.setProperty(“http.proxyHost”,”);//设置代理服务器
System.setProperty(“http.proxyPort”,”);//设置代理端口
Document doc=Jsoup.connect(“http://your.url.here“”。get();//Jsoup现在通过代理连接
或者将网站下载到字符串中并对其进行解析,然后:

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());
最终URL网站=新URL(“http://your.url.here"); // 您要连接的网站
//--通过代理设置连接
Proxy Proxy=新代理(Proxy.Type.HTTP,new InetSocketAddress(“,1234));//设置代理服务器和端口
HttpURLConnection httpUrlConnetion=(HttpURLConnection)website.openConnection(proxy);
httpUrlConnetion.connect();
//--将网站下载到缓冲区中
BufferedReader br=新的BufferedReader(新的InputStreamReader(httpUrlConnetion.getInputStream());
StringBuilder缓冲区=新的StringBuilder();
字符串str;
而((str=br.readLine())!=null)
{
buffer.append(str);
}
//--使用Jsoup解析缓冲区
Document doc=Jsoup.parse(buffer.toString());

对于此解决方案,您也可以使用
HttpClient

如果您的代理需要用户名/密码身份验证,则可以添加ollo

final String authUser = <username>;
final String authPassword = <password>;
Authenticator.setDefault(
   new Authenticator() {
      public PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication(
               authUser, authPassword.toCharArray());
      }
   }
);

System.setProperty("http.proxyHost", <yourproxyhost>);
System.setProperty("http.proxyPort", <yourproxyport>);
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);

Document doc = Jsoup.connect("http://your.url.here").get();
最终字符串authUser=;
最后一个字符串authPassword=;
Authenticator.setDefault(
新验证器(){
公共密码身份验证getPasswordAuthentication(){
返回新密码验证(
authUser,authPassword.toCharArray());
}
}
);
System.setProperty(“http.proxyHost”,);
setProperty(“http.proxyPort”,);
System.setProperty(“http.proxyUser”,authUser);
System.setProperty(“http.proxyPassword”,authPassword);
Document doc=Jsoup.connect(“http://your.url.here).get();

从1.9版开始,您可以在连接上进行设置:

JSoup.connect(“http://your.url.here“”.proxy(“,).get();

如果您有
HttpClient
在代理上运行,您可以使用它将网站下载到一个字符串中,并解析这个字符串(如我的答案中的解决方案#2)。
JSoup.connect("http://your.url.here").proxy("<proxy-host>", <proxy-port>).get();