BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 03-15-2011, 12:35 PM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Mar 2011
Model: 8900
PIN: N/A
Carrier: Torch
Posts: 59
Post Thanks: 3
Thanked 1 Time in 1 Post
Default Https Connection

Please Login to Remove!

Hello

I am opening Https url using HttpsConnection class & Connector.Open() but response code return by code is 302 not 200.

Can any body tells why response code is 320 not 200.

you can check whether there is some thing wrong wd my code. My code is as follow:-


StringBuffer sb = new StringBuffer();

HttpsConnection httpConn = null;
InputStream is = null;
OutputStream os = null;
int res=0;

try {


httpConn = (HttpsConnection) Connector.open(url);
httpConn.setRequestMethod(HttpsConnection.POST);

httpConn.setRequestProperty("User-Agent",
"Profile/MIDP-2.0 Confirguration/CLDC-1.0");
httpConn.setRequestProperty("Accept_Language", "en-US");
httpConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");


res=httpConn.getResponseCode();

is = httpConn.openDataInputStream();

int chr;
while ((chr = is.read()) != -1)
sb.append((char) chr);

} catch (IOException e) {
e.printStackTrace();
} finally {

try {
if (is != null)
is.close();
if (os != null)
os.close();
if (httpConn != null)
httpConn.close();

} catch (IOException e) {
e.printStackTrace();
}
}
return res+sb.toString();
}
Offline  
Old 03-15-2011, 12:41 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default Re: Https Connection

HTTP 302 is a redirect.

You should get the redirect URL in the "Location" header in the response.

You then re-connect to this new location.
Offline  
Old 03-15-2011, 12:45 PM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Mar 2011
Model: 8900
PIN: N/A
Carrier: Torch
Posts: 59
Post Thanks: 3
Thanked 1 Time in 1 Post
Default Re: Https Connection

How can I get the redirect URL in the "Location" header.
Can you please explain
In my code where i hv to make this change?
Offline  
Old 03-15-2011, 12:51 PM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: Mar 2011
Model: 8900
PIN: N/A
Carrier: Torch
Posts: 59
Post Thanks: 3
Thanked 1 Time in 1 Post
Default Re: Https Connection

Can you please explain in detail what you mean?
Offline  
Old 03-15-2011, 01:41 PM   #5 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default Re: Https Connection

You need to get the "Location" property header from the HTTP response.

String location = httpConnection.getHeaderField("Location");

This gives you the redirected URL. You then load this URL to replace whatever URL you originally were going after, and try again.

You understand that this is strictly HTTP 1.1 stuff, and documented in about 1,000,000 places, right? Try "google" searching for HTTP 302 and you will find plenty of reference material.
Offline  
The Following User Says Thank You to Dougsg38p For This Useful Post:
xmncxzvt (03-17-2011)
Old 03-17-2011, 12:25 PM   #6 (permalink)
Thumbs Must Hurt
 
Join Date: Mar 2011
Model: 8900
PIN: N/A
Carrier: Torch
Posts: 59
Post Thanks: 3
Thanked 1 Time in 1 Post
Thumbs up Re: Https Connection

Thanks Dougsg38p
I got your suggestion
This means that
String location =httpConnection.getHeaderField("Location"); gives redirected url.
Some sites provide their url which redirects to another url (actual url) or we
can say that some sites doesnt gave direct access to their url.So, we are
accessing those sites indirectly.when we enter url in browser it automatically access redirected url & opens page but in code when we create httpconnection & gave it in url, it doesnt automatically opens redirected url.

So, we get redirected url from getHeaderField() , create another httpconnection object & gave it redirected url like this:

HttpsConnection httpconn = null;
HttpsConnection hc = null;
InputStream is = null;


httpConn = (HttpsConnection) Connector.open(url);
httpConn.setRequestMethod(HttpsConnection.POST);

String location =httpConn.getHeaderField("Location");

hc = (HttpsConnection) Connector.open(location);
is = hc.openDataInputStream();

I havent implemented this change to my code yet. But I think now response
code must be 200 because i am creating another httpconnection object &
giving it redirected url.

Let me make this change to my code
Offline  
Old 03-17-2011, 01:16 PM   #7 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default Re: Https Connection

I think you have the picture!

One more issue to be aware of: if you are on a *real* device and you applicaiton is connecting via BES or BIS-B, then the server-side handler will actually make the redirect for you, and you'll never know that it happened.

On Direct TCP or WAP connections (or any conneciton where you have sert ";deviceside=true", you will have to handle the redirect in your code.

Anyway, it's always a good idea to handle this, just in case.

Good luck with your program.

Last edited by Dougsg38p : 03-17-2011 at 01:18 PM.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads for: Https Connection
Thread Thread Starter Forum Replies Last Post
BES 5.0 : How to allow untrusted HTTPS/TLS connection? johnnylq Developer Forum 9 09-07-2010 08:05 AM
Local Database Connection Error ogaal BES Admin Corner 10 11-25-2009 01:05 AM
HTTPS Connection ppittman BES Admin Corner 0 05-07-2009 05:52 AM
Need help - trouble accessing email through https connection kevdog_22 General 8300 Series Discussion - Curve 3 05-15-2008 06:08 AM
BES server that was configured at another site seems to require that site to work mannyp BES Admin Corner 7 07-05-2007 08:48 PM





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.