Eclipse出站连接被阻止:网页浏览器使用相同的网址 [英] Eclipse Outbound connection blocked : The same url works from web browser
问题描述
我正在使用HttpURLConnection类从eclipse连接到外部Web服务,然后我收到一条错误消息Connection Refused
I am using the HttpURLConnection class to connect to external web service from eclipse, then I am getting a error message "Connection Refused"
public class TestConnection {
static {
//for localhost testing only
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
new javax.net.ssl.HostnameVerifier(){
public boolean verify(String hostname,
javax.net.ssl.SSLSession sslSession) {
if (hostname.equals("localhost")) {
return true;
}
return false;
}
});
}
public static void main(String[] args)
{
HttpURLConnection urlConnection;
try {
//https get method with required parameters
// XMLHttpRequest s1 = new XMLHttpRequest ();
urlConnection = (HttpURLConnection) ((new URL("https://google.com").openConnection()));
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("User-Agent", "");
int HTTPS_RESPONSE_CODE = urlConnection.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String HTTPS_RESULT=response.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at TestConnection.main(TestConnection.java:19)
但是如果我尝试从浏览器连接到同一个网站,我可以从服务中获得响应。
But If i try to connect to the same site from a browser I am able to get a response from the service. Can you please let me know if there is a work around?
推荐答案
请找到工作代码
PROXY_SERVER设置为一个有效的代理服务器和我正在使用的http端口是8080
PROXY_SERVER set to a valid proxy server and port for http I am using is 8080
proxy = new Proxy(Proxy.Type.HTTP ,新的InetSocketAddress(PROXY_SERVER,PROXY_PORT));
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_SERVER, PROXY_PORT));
当您建立连接通过代理作为参数时。
And when you establish the connection pass the proxy as an argument.
urlConnection =(HttpURLConnection)((新URL(URI).openConnection(proxy)));
urlConnection = (HttpURLConnection) ((new URL(URI).openConnection(proxy)));
这篇关于Eclipse出站连接被阻止:网页浏览器使用相同的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!