Java connection failure using web proxy using Apache HttpClient

I have a desktop client that sends data to a web server and I cannot go through a proxy server.

Update : I get an HTTP 407 error when trying to communicate through a proxy.

When downloading information from my web server, everything is in order. When the user configures the proxy server (using the dialog box I wrote), the download works fine. But loading data using org.apache.http.client.HttpClient does not work.

I am setting up a proxy server with code like this after collecting information from JDialog.

        System.setProperty("http.proxyHost", proxyHost);
        System.setProperty("http.proxyPort", "" + portNumber);

Now that we have done this, simple downloads work fine. For example, I have code that reads some XML data from my web server (see below). On the clientโ€™s network, an error in my catch block was displayed before the proxy server settings were configured, and then everything worked fine when the correct proxy was installed.

/**
 * Loads a collection of exams from the web site. The URL is determined by
 * configuration or registration since it is State specific.
 */
public static int importExamsWS(StringBuilder msg) {
    try {
        java.net.URL onlineExams = new URL(examURL);
        //Parse the XML data from InputStream and store it.
        return importExams(onlineExams.openStream(), msg);

    } 
    catch (java.net.UnknownHostException noDNS) {
        showError(noDNS, "Unable to connect to proctinator.com to download the exam file.\n"
                + "There is probably a problem with your Internet proxy settings.");
    }
    catch (MalformedURLException | IOException duh) {
        showFileError(duh);
    }
    return 0;
}

However, when I try to transfer data to a web server, it looks like proxy settings are ignored and an IOException is thrown. Namely:

org.apache.http.conn.HttpHostConnectException: Connection to http://proctinator.com:8080 refused

Now I know that port 8080 is not blocked by the clientโ€™s web filter, because we tested it in a web browser.

Here is my code for checking the registration ID entered by the user:   Update: now I also install the proxy server in this method.

//Registered is just an enum with ACTIVE, INACTIVE, NOTFOUND, ERROR
public static Registered checkRegistration(int id) throws IOException {    
    httpclient = new DefaultHttpClient();
    Config pref = Config.getConfig(); //stores user-entered proxy settings.
    HttpHost proxy = new HttpHost(pref.getProxyServer(), pref.getProxyPort());
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    URIBuilder builder = new URIBuilder();
    String path = "/Proctorest/rsc/register/" + id;
    try {
        builder.setScheme("http").setHost(server).setPort(8080).setPath(path);
        URI uri = builder.build();
        System.out.println("Connecting to " + uri.toString());
        HttpGet httpget = new HttpGet(uri);
        HttpResponse response = httpclient.execute(httpget);
        System.out.println(response.getStatusLine().toString());
        if(response.getStatusLine().getStatusCode()==200) {
            String msg = EntityUtils.toString(response.getEntity());
            GUI.globalLog.log(Level.INFO, "Server response to checkRegistration(" + id + "): " + msg);
            return Registered.stringToRegistered(msg);
        }
        else {
            GUI.globalLog.log(Level.INFO, "Server response status code to checkRegistration: " + 
                    response.getStatusLine().getStatusCode());
            return Registered.ERROR;
        }
    }
    catch(java.net.URISyntaxException bad) {
        System.out.println("URI construction error: " + bad.toString());
        return Registered.ERROR;
    }
}

, -, docs SystemDefaultHttpClient , http.proxyHost http.proxyPort. , , . , , :

checkRegistration INFO: Server response status code to checkRegistration: 407 

?

+5
3

. - , :

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
    new AuthScope("PROXY HOST", 8080),
    new UsernamePasswordCredentials("username", "password"));

HttpHost targetHost = new HttpHost("TARGET HOST", 443, "https");
HttpHost proxy = new HttpHost("PROXY HOST", 8080);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 

- NTLM, , NTLM - ( - NTLM - , - v1 v2 NTLM), :

http://hc.apache.org/httpcomponents-client-ga/ntlm.html

http://htmlunit.sourceforge.net/ntlm.html

http://devsac.blogspot.com/2010/10/supoprt-for-ntlmv2-with-apache.html

- NTCredentials UserPasswordCredentials.

, , , wirehark -, , .

+5

, JVM http.proxy, - JVM :

System.setProperty("http.proxyUser",proxyUserName)
System.setProperty("http.proxyPassword",proxyUsePassword).
+1

For some reason, setting the parameters on the client did not work for me.

But worked on the http method.

0
source

All Articles