Tudor
Tudor

Reputation: 209

Error 407 when trying to send a request in Java

I have the following code:

public class SendRequest {

public static void main(String[] args) throws ClientProtocolException, IOException {

    String url = "http://backoffice.xyz";

    HttpHost proxy = new HttpHost("proxy.proxy", 8080, "http");
    HttpClient client = HttpClientBuilder.create().setProxy(proxy).build();
    HttpGet request = new HttpGet(url);

    //request.addHeader("User-Agent", "USER-AGENT");
    HttpResponse response = client.execute(request);

    System.out.println("Response Code: " + response.getStatusLine().getStatusCode());

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while((line = rd.readLine()) != null){
        result.append(line);
    }
        System.out.println(result.toString());

}

}

This is returning a 407 Unauthorized Access/Cache Access Denied Error. What code do i need to include so i can authenticate through the proxy?

Upvotes: 0

Views: 429

Answers (2)

Gautam
Gautam

Reputation: 564

Does your proxy require username/password based authentication? If so, try implementing java.net.Authenticator. I guess you will need to set useSystemProperties

Authenticator.setDefault(new Authenticator(){ PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(username, password); } });

You might need to add setDefaultCredentialsProvider(CredentialsProvider credentialsProvider) to the HTTPClientBuilder and use SystemDefaultCredentialsProvider instance for that.

Upvotes: 1

karthik
karthik

Reputation: 17

1.Can you check your proxy ?? I'm not sure if proxy.proxy is correct.

Your rest of the code seems fine.

2.And also make sure you are updating httpcore version to 4.4 or above. And also you can update httpcore to latest version.

3.You could check the authentication of your proxy as @Goutham mentioned

Hope this helps!!

Upvotes: 0

Related Questions