1

I'm trying to use HTTP post to send a file to a web server that requires basic authentication.

My workplace has recently implemented a change to the proxy server and it now also requires basic authentication.

How can I enter my credentials for both of these servers in a single HttpPost request?

2 Answers 2

3

Turns out I needed to add a "Proxy-Authorization" header as well.

HttpPost httpPost = new HttpPost("http://host:port/test/login");

String encoding = Base64Encoder.encode ("your_user:your_password");
httpPost.setHeader("Authorization", "Basic " + encoding);

String proxyEncoding = Base64Encoder.encode ("proxy_user:proxy_password");
httpPost.setHeader("Proxy-Authorization", "Basic " + proxyEncoding);

System.out.println("executing request " + httpPost.getRequestLine());
HttpResponse response = httpClient.execute(httpPost);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add in your http header following line, as described in basic authorization:

  1. The username and password are combined with a single colon.

  2. The resulting string is encoded using the RFC2045-MIME variant of Base64, except not limited to 76 char/line.

  3. The authorization method and a space i.e. "Basic " is then put before the encoded string.

Look at example with Apache HttpClient:

String encoding = Base64Encoder.encode ("your_login:your_password");
HttpPost httppost = new HttpPost("http://host:post/test/login");
httppost.setHeader("Authorization", "Basic " + encoding);

System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

1 Comment

So the login and password are for the web server. What about the username and password of the proxy server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.