-1

I am using system.properties in my java code. It works perfect on a windows 7 OS but fails on ubuntu12.04.

I used tomcat at both the places. Any help in this regard will be helpful.

System.setProperty("http.proxyHost", proxyhost);
System.setProperty("http.proxyPort", proxyport);        
String encoded = new String(encoder.encode(new String(username+":"+password).getBytes()));
uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);

Exception: java.io.IOException: Server returned HTTP response code: 401 for URL: http://

  • http://stackoverflow.com/questions/10479434/server-returned-http-response-code-401-for-url-https this might help – Ariel Pinchover Apr 15 '13 at 15:20
  • In the absence of a defined charset the `getBytes()` method will use the platform's default charset to return the `byte[]`. It is possible that information is lost in this process and the wrong base64 encoded `BASIC` auth is sent to the proxy. I'd begin by checking that. – Deepak Bala Apr 15 '13 at 15:32
  • Thanks all only UTF-8 was missing – Naegsh Pachorkar Apr 16 '13 at 06:24

1 Answers1

-1

The 401 error code indicates "Unauthorized". You could try this:

System.setProperty("http.proxyHost", "yourproxyhost");
System.setProperty("http.proxyPort", "yourproxyport");

String credentials = username + ":" + password;
String encoded  = Base64Converter.encode(credentials.getBytes("UTF-8"));
URLConnection uc = url.openConnection();
uc.setRequestProperty("Proxy-Authorization", String.format("Basic %s", encoded));
1218985
  • 7,531
  • 2
  • 25
  • 31