我有下面的代碼片斷:的ConnectException一臺計算機上而不是在另一個
private static String post(String msg) throws Exception {
URL urlObj = new URL(host);
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
System.out.println(String.format("Send to url [%s]", urlObj));
System.out.println(String.format("Port [%s]", con.getURL().getDefaultPort()));
System.out.println(String.format("POST data [%s]", msg));
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(msg);
wr.flush();
wr.close();
int respCode = con.getResponseCode();
System.out.println(String.format("Response code [%s]", respCode));
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String response = "", s;
while ((s = in.readLine()) != null) {
response += s;
}
in.close();
return response;
}
此方法是一個程序,它接受一個YYYYMMDD日期字符串,把它封裝在JSON的一部分,加密在JSON串base 64,然後將其作爲查詢發送到https Web服務。
我在我的個人筆記本電腦上運行程序,連接到互聯網,並且能夠成功查詢和處理結果。我還可以訪問Web服務URL並手動提交查詢。
然而,我的服務器,通過代理連接到互聯網上,我仍然可以手動通過瀏覽器查詢的Web服務,但是當我運行同一程序現在我得到一個異常:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625)
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1092)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at com.chinkai.EvaWSAgent.post(EvaWSAgent.java:87)
at com.chinkai.EvaWSAgent.retrieve(EvaWSAgent.java:50)
at com.chinkai.EvaWSAgent.main(EvaWSAgent.java:155)
...其中第87行是
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
我不知道如何解決這個,或者如果有什麼辦法可以重寫這個方法來解決,或者如果它甚至我在首位的問題(設置在代理中,也許?)不知道這是否是提出這個問題的正確位置,無論如何,我都在努力運氣。
請輸入,如果你沒有連接到URL,則意味着特定的URL訪問你的服務器堵塞 –