Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HttpURLConnection through proxy: Issues with getting encoded password

116 views
Skip to first unread message

David M. Karr

unread,
Apr 24, 2003, 7:31:57 PM4/24/03
to
I'm struggling with a situation where I need to use HttpURLConnection
to connect to an outside host, through a proxy. I'm using JDK 1.2.2
(bleah).

First of all, I'm a little confused on exactly what properties I'm
supposed to set for the proxy host and port. I've seen several
different variations on the properties and names I'm supposed to set.
I'm currently using "proxySet", "proxyHost", and "proxyPort".

As I have to get the user's proxy login and password, I'm currently
using a plain form to gather that and forward to the servlet (although
I'm doing basic testing just typing in my parameters in the browser
URL field). Eventually I hope to have a "service account" whose only
purpose is to be the proxy login and password for this.

My code for encoding the proxy login and password is as follows:

String encodedString =
new BASE64Encoder().
encodeBuffer((proxyLogin + ":" + proxyPassword).getBytes());

httpURLConnection.
setRequestProperty("Proxy-Authorization", "Basic " + encodedString);

When I do this, however, I get the following:

java.lang.IllegalArgumentException: Illegal character(s) in message
header value: Basic ......................==

I replaced the alphanumeric text in the encoded password with periods,
except for the last two characters.

What could I be doing wrong?

David M. Karr

unread,
Apr 25, 2003, 11:51:37 AM4/25/03
to
david...@wamu.net (David M. Karr) wrote in message news:<cafe491.03042...@posting.google.com>...

> My code for encoding the proxy login and password is as follows:
>
> String encodedString =
> new BASE64Encoder().
> encodeBuffer((proxyLogin + ":" + proxyPassword).getBytes());
>
> httpURLConnection.
> setRequestProperty("Proxy-Authorization", "Basic " + encodedString);
>
> When I do this, however, I get the following:
>
> java.lang.IllegalArgumentException: Illegal character(s) in message
> header value: Basic ......................==
>
> I replaced the alphanumeric text in the encoded password with periods,
> except for the last two characters.

One thing I've discovered is that the encoded string generated by
"sun.misc.BASE64Encoder" actually ends with a newline character. When
I removed the newline from the end of the string, it appeared to work
better (although now I appear to have a DNS issue). Is it expected
that BASE64Encoder produces results that are not all base 64
characters?

Stephen Ostermiller

unread,
Apr 27, 2003, 8:30:31 AM4/27/03
to
Whitespace is expected in Base64 encoding. All decoders should ignore
any whitespace. There should be a new line in Base64 data ever 72
characters to prevent line wrapping when large base64 data is sent
through email.

Your first mistake is using the Sun class. It is undocumented,
subject to change, and is not included with distributions from
Microsoft or IBM. Furthermore, Sun has said that it should not be
used.

There are implementations of Base64 encoding that allow you to leave
out the whitespace. I have written one and I keep a list of others.
You can find that information here:
http://ostermiller.org/utils/Base64.html

Stephen

David M. Karr

unread,
Apr 29, 2003, 7:50:42 PM4/29/03
to
I'm making progress with this (I think). I'm now using the
Authenticator class, which I assume builds the "Proxy-Authentication"
header behind the scenes.

However, I'm now getting the following exception:

--------------------
java.io.IOException: Unable to tunnel through proxy. Proxy returns
"HTTP/1.1 400 Bad Request"
at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:813)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(DashoA6275)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:528)
at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getOutputStream(DashoA6275)
--------------------

My code looks approximately like this:

--------------------
System.setProperty("java.protocol.handler.pkgs",

"com.sun.net.ssl.internal.www.protocol");

if (proxyHost != null)
{
System.setProperty("https.proxySet", "true");
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", proxyPort + "");

proxyLogin = request.getParameter("proxyLogin");
proxyPassword = request.getParameter("proxyPassword");

Authenticator authenticator =
new Authenticator() {
public PasswordAuthentication
getPasswordAuthentication() {
return (new
PasswordAuthentication(proxyLogin,

proxyPassword.

toCharArray()));
}};
Authenticator.setDefault(authenticator);
}

HttpsURLConnection connection =
(HttpsURLConnection) (new
URL(gatewayURL)).openConnection();

connection.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String host, String cert) {
System.out.println("verify. host[" + host +
"] cert[" + cert + "]");
return true;
}});

connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setFollowRedirects(false);
connection.setAllowUserInteraction(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

String requestParamsString =
"RequestorVersion=simple" +
"&PartnerUsername=" +
URLEncoder.encode(partnerUserName) +
"&PartnerPassword=" +
URLEncoder.encode(partnerPassword) +
"&Username=" + URLEncoder.encode(unameHeaderValue);
connection.setRequestProperty("Content-length",
requestParamsString.length()
+ "");

System.out.println("usingProxy[" + connection.usingProxy()
+
"]");
DataOutputStream outputStream =
new DataOutputStream(connection.getOutputStream());
--------------------

David M. Karr

unread,
May 2, 2003, 11:30:19 PM5/2/03
to
>>>>> "David" == David M Karr <david...@wamu.net> writes:

David> I'm struggling with a situation where I need to use HttpURLConnection
David> to connect to an outside host, through a proxy. I'm using JDK 1.2.2
David> (bleah).

David> First of all, I'm a little confused on exactly what properties I'm
David> supposed to set for the proxy host and port. I've seen several
David> different variations on the properties and names I'm supposed to set.
David> I'm currently using "proxySet", "proxyHost", and "proxyPort".

David> As I have to get the user's proxy login and password, I'm currently
David> using a plain form to gather that and forward to the servlet (although
David> I'm doing basic testing just typing in my parameters in the browser
David> URL field). Eventually I hope to have a "service account" whose only
David> purpose is to be the proxy login and password for this.

David> My code for encoding the proxy login and password is as follows:

David> String encodedString =
David> new BASE64Encoder().
David> encodeBuffer((proxyLogin + ":" + proxyPassword).getBytes());

David> httpURLConnection.
David> setRequestProperty("Proxy-Authorization", "Basic " + encodedString);

David> When I do this, however, I get the following:

David> java.lang.IllegalArgumentException: Illegal character(s) in message
David> header value: Basic ......................==

David> I replaced the alphanumeric text in the encoded password with periods,
David> except for the last two characters.

David> What could I be doing wrong?

Although my app still has a problem or two, I believe I've gotten past all the
connection problems. The best thing I did was integrate the Commons HttpClient
library. It makes all of this very straightforward.

--
===================================================================
David M. Karr ; Java/J2EE/XML/Unix/C++
dmk...@earthlink.net ; SCJP; SCWCD


0 new messages