简体   繁体   中英

Java Application Proxy Connection Refused

I've been fighting this problem for the past day and a half and finally broke down after my searching failed to produce anything to resolve my issue.

I have a Spring-boot application that makes SOAP calls to a remote server. Everything works great from with STS, or running locally on my development laptop (either through STS>Run, mvn spring-boot:run, or running the jar file locally. When I deploy the code (a fat jar) to our corp development server all of my requests fail with "Connection refused (Connection refused)".

To triage I made the same request via Curl on the development server to see where the problem lies:

BARE REQUEST:

curl http://some.remote.server

--> curl: (7) Failed connect to :80; Connection refused

REQUEST WITH PROXY SETTINGS/AUTH:

export http_proxy=http://<proxy_user>:<proxy_pass>@<proxy_host>:<proxy_port>
curl -x $http_proxy http://some.remote.server

--> Success!

So at this point I'm pretty sure that the problem I'm seeing is that my calls to aren't getting routed through the proxy.

I've tried to add the proxy settings to the code, but I don't like the idea of reducing the portability (besides, it didn't work).

I've tried setting the parameters on the command line via:

java -Dhttp.proxyHost=<corp proxy host addr> -Dhttp.proxyPort=3128 -Dhttp.proxyUser=<proxy username> -Dhttp.proxyPassword=<proxy pass> -jar Application.jar

Nothing I'm doing makes a bit of difference.

What other tools / techniques can I use to diagnose / resolve this issue I'm seeing? I'm running out of hair to pull!

[note: eventually this will run from a Docker container, but I have removed as much of the moving parts as I can to simplify the solution. I think I'm doing something really basic incorrectly as I'm sure this isn't a unique problem.]

public String makeTestRequest()
{
    try
    {
        final String authUser = PROXY_USER;
        final String authPassword = PROXY_PASS;
        Authenticator.setDefault(
           new Authenticator() {
              @Override
              public PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication(
                       authUser, authPassword.toCharArray());
              }
           }
        );

        System.setProperty("http.proxyUser", PROXY_USER);
        System.setProperty("http.proxyPassword", PROXY_PASS);
        System.setProperty("http.proxyHost", PROXY_HOST);
        System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));


        URL url = new URL(REMOTE_URL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        System.out.println("Proxy? " + con.usingProxy());   // proxy?
        con.setRequestMethod("GET");

        int status = con.getResponseCode();
        if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM) 
        {
            String location = con.getHeaderField("Location");
            URL newUrl = new URL(location);
            con = (HttpURLConnection) newUrl.openConnection();
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer content = new StringBuffer();
        while ((inputLine = in.readLine()) != null) 
        {
            content.append(inputLine);
        }

        // cleanup / return
        in.close();
        con.disconnect();
        return content.toString();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return e.getMessage();
    }
}

To anyone coming across this issue and struggling, here is the code which worked for me:

package proxycheck;


import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;



public class ConnectionTest 
{
    private static final String PROXY_HOST_URI = "mycompanyproxy.corp.net";
    private static final int    PROXY_PORT = 7283;
    private static final String TEST_URL   = "http://some.remote.service.com/something";

    public static void main(String[] args) 
    throws IOException 
    {
        Authenticator.setDefault(new EdlsAuthenticator());
        HttpURLConnection conn;
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST_URI, PROXY_PORT));

        URL httpUrl = new URL(TEST_URL);
        conn = (HttpURLConnection) httpUrl.openConnection(proxy);

        System.out.println(conn.getResponseCode());
        System.out.println(readResponse((InputStream) conn.getContent()));
    }


    public static String readResponse (InputStream in) 
    throws IOException 
    {
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = in.read(b)) != -1;) 
        {
            out.append(new String(b, 0, n));
        }
        return out.toString();
    }

}




class EdlsAuthenticator extends Authenticator 
{
    protected PasswordAuthentication getPasswordAuthentication() 
    {       
        String username = "ProxyUsername";
        String password = "ProxyPassword";
        return new PasswordAuthentication(username, password.toCharArray());
    }
}

My pom.xml contains the following dependencies:

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.9</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.3.2</version>
</dependency>
<dependency>
    <groupId>org.soulwing.ssl</groupId>
    <artifactId>ssl-context-tools</artifactId>
    <version>1.0.1</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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