简体   繁体   English

使用Java,需要通过代理建立https连接

[英]Using Java, Need to establish an https connection via proxy

I need to establish and send/read over/from an https connection (to a website of course) but through an http proxy or SOCKS proxy. 我需要通过HTTP代理或SOCKS代理建立和发送/从https连接(当然是网站)/从中读取信息。 A few other requirements 其他一些要求

  • supports blocking (I can't use non-blocking/nio) 支持阻塞(我不能使用非阻塞/ NIO)
  • isn't set as an environment or some other global scope property (there are multiple threads accessing) 未设置为环境或其他全局范围属性(有多个线程正在访问)

I was looking into HttpCore components but I did not see any support for blocking https. 我正在研究HttpCore组件,但没有看到对阻止https的任何支持。

Look at the java.net.Proxy class. 查看java.net.Proxy类。 That does what you need. 那就是您需要的。 You create one, and then pass it to the URLConnection to create the connection. 您创建一个,然后将其传递给URLConnection以创建连接。

To support per-thread proxy, your best bet is Apache HttpClient 4 (Http Components Client). 为了支持每线程代理,最好的选择是Apache HttpClient 4(Http组件客户端)。 Get the source code, 获取源代码,

http://hc.apache.org/downloads.cgi http://hc.apache.org/downloads.cgi

It comes with examples for both HTTP proxy and SOCKS proxy, 随附HTTP代理和SOCKS代理的示例,

   ClientExecuteProxy.java
   ClientExecuteSOCKS.java

Did you look at Apache HTTP Client ? 您看过Apache HTTP Client吗? Haven't used it in ages but I did use it to pick a proxy server dynamically. 很久没有使用它了,但我确实使用它来动态选择代理服务器。 Example from site here: 来自网站的示例:

 HttpClient httpclient = new HttpClient();
  httpclient.getHostConfiguration().setProxy("myproxyhost", 8080);
  httpclient.getState().setProxyCredentials("my-proxy-realm", " myproxyhost",
  new UsernamePasswordCredentials("my-proxy-username", "my-proxy-password"));
  GetMethod httpget = new GetMethod("https://www.verisign.com/");
  try { 
    httpclient.executeMethod(httpget);
    System.out.println(httpget.getStatusLine());
  } finally {
    httpget.releaseConnection();
  }
System.setProperty("http.proxyHost", "proxy.com");
System.setPropery("http.proxyPort", "8080");

URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();

http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM