Problem posting data with HttpsConnection

Hi,

I am currently having problems posting data using a HttpsConnection.

I am setting the setRequestMethod to HttpConnection.POST and then using the openDataOutputStream method.

I will post my code when I get home for reference.

Background to my problem - I am trying to use the declarative security features of J2EE to authenticate a mobile user.

I am firstly firing a HttpsConnection from the mobile device to a protected resource on the app server. I grab the JSessionId from the Set-Cookie http header of the response and post a second request to the j_security_check and provide the refer http header to the protected resource - this simulates exactly what happens on a browser (I used a Http monitor tool to confirm this). However, I need to pass the j_username and j_password as body of the https request. This doesn't seem to work because the server displays my access denied at this point - as if it hadn't received the crednetials

However, just to confirm my technique works I subsituted the post request for a get request to j_security_check and passed the j_username and j_password as request params in the URL. This worked fine and I got a 302 response 'temporily moved' which I then handle by simply making a further request for the original resource - this time it serves back the resource because I am authenticated.

Anyway that sets the scene - so the problem definitely appears to be posting data using an HttpsConnection.

Any help would be greatly appreciated

Rgds,

David

[1562 byte] By [davidturner90a] at [2007-10-3 2:51:32]
# 1
What code are you using to write the username and passwordt POST data?
deepspacea at 2007-7-14 20:40:25 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

Hi,

Here is my code relating to the above problem:

....

httpsConn = (HttpsConnection)Connector.open("https://127.0.0.1:8443/ebank/j_security_check");

httpsConn.setRequestMethod(HttpConnection.POST);

httpsConn.setRequestProperty("referer", "https://127.0.0.1:8443/ebank/main/mainMenu.xml");

httpsConn.setRequestProperty("cookie", cookieValue);

httpsConn.setRequestProperty("location", url);

httpsConn.setRequestProperty("User-Agent",

"Profile/MIDP-1.0, Configuration/CLDC-1.0");

httpsConn.setRequestProperty("Content-Language",

"en-gb");

String credentials = "j_username=EB0001&j_password=240589";

httpsConn.setRequestProperty("Content-Length",""+credentials.length());

DataOutputStream os = httpsConn.openDataOutputStream();

os.writeUTF(credentials);

....

Thanks,

David

davidturner90a at 2007-7-14 20:40:25 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

Fixed it!!

Two amendments required to the above code:

1. Add the Http header Content-Type to tell the server this is a form: httpsConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

2. Change from using the writeUTF on java.io.DataOutputStream to the write method and pass credentials as a byte[].

All I can think here is that UTF-8 encoding is not liked ?

Any ideas?

Thanks,

Dave

davidturner90a at 2007-7-14 20:40:25 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

> 2. Change from using the writeUTF on

> java.io.DataOutputStream to the write method and pass

> credentials as a byte[].

I knew it, classic mistake!

> All I can think here is that UTF-8 encoding is not

> liked ?

No, if you convert your String to byte it will also be UTF8 data. Just look at the writeUTF api docs, you'll see what's wrong!

deepspacea at 2007-7-14 20:40:25 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

I am trying to do the same thing regarding j_security_check and I have been unsuccessful up to this point. Is there anyway I could convince you to post your working solution so that I can figure out what I am doing wrong?

No matter what I try I always get an unsuccesful login.

Thanks in advance,

Keith

Shaftoea at 2007-7-14 20:40:25 > top of Java-index,Java Mobility Forums,Java ME Technologies...