ConnectException在一台机器上,但在另一台机器上没有

时间:2017-07-31 09:53:26

标签: java 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中,加密base 64中的JSON字符串,然后将其作为查询发送到https Web服务。

我在个人笔记本电脑上运行程序,连接到Internet,并能够成功查询和处理结果。我还可以访问Web服务URL并手动提交查询。

但是,在我通过代理连接到Internet的服务器上,我仍然可以通过浏览器手动查询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());

我不确定如何解决这个问题,或者我是否可以通过任何方式重写此方法来解决问题,或者如果它首先解决了我的问题(设置在代理人,也许?)不确定这是否是提出这个问题的正确位置,但无论如何我都在尝试运气。

2 个答案:

答案 0 :(得分:1)

如果服务器通过代理连接到互联网,您需要在应用程序中设置它,例如那样:

URL urlObj = new URL(host);

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.yourdomain.com", 3128));
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection(proxy);

答案 1 :(得分:0)

您可以按照其他人的建议在代码中设置代理。您还可以在运行Java应用程序时从命令行进行设置。

FROM elixir:1.4.5
COPY . /
RUN mix compile
CMD echo "Application started" &&  elixir --name $MY_POD_NAMESPACE@$MY_POD_IP --no-halt --cookie $ERLANG_COOKIE -S mix run