在 Java 中通过代理获取 SSL 页面的最简单方法
我想用 Java 获取 SSL 页面。 问题是,我必须针对 http 代理进行身份验证。
所以我想要一个简单的方法来获取这个页面。 我尝试了 Apache Commons httpclient,但对于我的问题来说它的开销太大了。
我尝试了这段代码,但它不包含身份验证操作:
import java.io.*;
import java.net.*;
public class ProxyTest {
public static void main(String[] args) throws ClientProtocolException, IOException {
URL url = new URL("https://ssl.site");
Socket s = new Socket("proxy.address", 8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP, s.getLocalSocketAddress());
URLConnection connection = url.openConnection(proxy);
InputStream inputStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String tmpLine = "";
while ((tmpLine = br.readLine()) != null) {
System.out.println(tmpLine);
}
}
}
任何人都可以提供一些如何以简单的方式实现它的信息吗?
提前致谢
I would like to fetch a SSL page in Java. The problem is, that I have to authenticate against a http proxy.
So I want a simple way to fetch this page.
I tried the Apache Commons httpclient, but it's too much overhead for my problem.
I tried this piece of code, but it does not contain an authentication action:
import java.io.*;
import java.net.*;
public class ProxyTest {
public static void main(String[] args) throws ClientProtocolException, IOException {
URL url = new URL("https://ssl.site");
Socket s = new Socket("proxy.address", 8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP, s.getLocalSocketAddress());
URLConnection connection = url.openConnection(proxy);
InputStream inputStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String tmpLine = "";
while ((tmpLine = br.readLine()) != null) {
System.out.println(tmpLine);
}
}
}
Can anyone provide some information how to implement it on an easy way?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要设置 java.net.Authenticator 在打开连接之前:
要在完成后删除身份验证器,请调用以下代码:
Java SE 6 中的身份验证器支持
HTTP Basic
、HTTP Digest
和NTLM
。 有关详细信息,请参阅Http 身份验证 sun.com 上的文档You need to set a java.net.Authenticator before you open your connection:
To remove your authenticator after you're finished, call the following code:
The authenticator in Java SE 6 supports
HTTP Basic
,HTTP Digest
andNTLM
. For more information, see the Http Authentication documentation at sun.comorg.apache.commons.httpclient.HttpClient 是你的朋友,
示例代码来自 http: //hc.apache.org/httpclient-3.x/sslguide.html
org.apache.commons.httpclient.HttpClient is your friend,
Sample code from http://hc.apache.org/httpclient-3.x/sslguide.html
使用 apache commons-http-client 4 :
你会发现很多例子@ https://github.com/apache/httpcomponents-client/tree/master/httpclient5/src/test/java/org/apache/hc/client5/http/examples
特别是 https://github.com/apache/httpcomponents-client/blob/master/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientProxyAuthentication.java
with apache commons-http-client 4 :
you'll find a lot of examples @ https://github.com/apache/httpcomponents-client/tree/master/httpclient5/src/test/java/org/apache/hc/client5/http/examples
and especially https://github.com/apache/httpcomponents-client/blob/master/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientProxyAuthentication.java