Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
pascallambert
Posts: 24
Registered: ‎08-07-2008

HttpConnection how to?

Ok I'm lost!

 

I've a MIDlet that send data through an HttpConnection POST on some web server. On the open I get the following error:

java.lang.SecurityException: javax.microedition.io.Connector.http 

I've read the following documentation that explain the different scenario of a connection. I think I fall in the "HTTP connectivity using a service provide-hosted IP gateway"

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/7979/1181821/832210/Managing_Wireless_Data_Transport_on_BlackBerry_4_-_Part_1.pdf?nodeid=1202447&vernum=0

 

But If I do an HTTP GET it's ok?! Why? 

 

The other question is : Do I need to code something special base on the type of connectivity? 

 

Please use plain text.
Developer
duksunkim
Posts: 141
Registered: ‎08-06-2008

Re: HttpConnection how to?

Hi,

 

I'm having similar problem.

From the simulator I can not make http post using below code.

====================================

private void httpPost() {

HttpConnection conn = null;OutputStream out =

null; int responseCode;

 

try {System.

out.println("URL: " + url);

conn = (HttpConnection) Connector.open(url);conn.setRequestMethod(HttpConnection.

POST);

out = conn.openOutputStream();

out.write(
data.getBytes());

out.flush();

responseCode = conn.getResponseCode();

 

httpRespCode = responseCode;

if (responseCode != HttpConnection.HTTP_OK) {

sendResult = false;

} else {

sendResult = true;

}

start = false; sending = false;

 

} catch (IOException e) {

start = false;

sendResult = false;

sending = false;

}

 

}

=====================================

Although I tried to capture it through network monitor, no traffic happened. I used http demo version which came with simulator but no luck.

Can anyone help us?

 

Thanks,

DK

Please use plain text.
Developer
peter_strange
Posts: 19,508
Registered: ‎07-14-2008

Re: HttpConnection how to?

duksunkim:

I presume that you don't have a the same problem as the previous poster, i.e.. GET works but POST doesn't.  If you do, ignore the following:

 

I'm guessing you have started the MDS Simulator.  You should review:

What Is - Different ways to make an HTTP or socket connection
Article Number: DB-00396

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is...

Your URL (which it would be useful to know), probably doesn't have ";deviceside=true" suffix, in which case the default will be to connect via the MDS, which means the device emulator will try to connect to the MDS simulator.

 

pascallambert:

Sorry I don't have an possible answer for you.  In summary, is it correct to say you can GET but not POST to the same web site? 

 

Both:

Can you tell us the URLs you use in both cases, and if the problem is on a device or the simulator or both.  Also the OS Levels involved (JDE, device and simulator).

 

Please use plain text.
New Developer
pascallambert
Posts: 24
Registered: ‎08-07-2008

Re: HttpConnection how to?

The website I'm testing on is a simple Servlet I made that handle both POST and GET. Actually I fix my problem appending at the end of the request ";deviceside=true". With this parameter I can do a POST without it I can't !!??? What is the meaning of that parameter?

 

I'm testing all this on a Blackberry 8700 os 4.2.1. I didn't test it on the simulator.

 

Cheers

Please use plain text.
Developer
duksunkim
Posts: 141
Registered: ‎08-06-2008

Re: HttpConnection how to?

Hello,

 

I modified the URL as  

http://65.98.94.68/mobile/mycom/controller.php?action_type=1&email=duksunkim@hotmail.com&password=[B....

Is this right format for GET/POST?

 

I'm still not getiing to work.

 

Thanks,

DK.

Please use plain text.
Developer
peter_strange
Posts: 19,508
Registered: ‎07-14-2008

Re: HttpConnection how to?

pascallambert:

Please review the link I gave earlier.  It explains the various 'suffix' parameters you add to a URL and how they are used.

 

duksunkim:

That looks like a GET rather than a POST URL, but I'm no http expert, in fact I'm not even a novice.  I'll consider my job done when you can see the data passing thoguh to the target URL, the formatting I will leave to someone else who knows more.

Please use plain text.
Developer
richard_puckett
Posts: 191
Registered: ‎04-03-2008

Re: HttpConnection how to?

[ Edited ]

"http://65.98.94.68/mobile/mycom/controller.php?action_type=1&email=duksunkim@hotmail.com&password=[B..."

 

If it's a POST then you should pass in the request parameters via setRequestProperty rather than putting them on the same line as the URL (as you do with GET).  If you were to do it by hand it would be:

 

telnet 65.98.94.68 80

POST /mobile/mycom/controller.php HTTP/1.1

action_type=1

email=duksunkim

password=s3cret

<then double enter>

 

So, in code your URL would be http://65.98.94.68 80;deviceside=true and you'd then push out your request params one by one.  Note also that it looks like you're trying to pass a byte array object ref as your password which probably isn't what you want.

 

I don't know why any of this would be causing catastrophic failure of your HTTPConnection though especially since you're going direct.

Message Edited by richard_puckett on 08-15-2008 09:23 AM
Please use plain text.
Developer
duksunkim
Posts: 141
Registered: ‎08-06-2008

Re: HttpConnection how to?

So format should be like  http://65.98.94.68:80;deviceside=true

Here is the code:

 

url = "http://65.98.94.68/mobile/myconcierge/controller.php;deviceside=true";

data = "action_type=1&email=duksunkim@hotmail.com&password=[B@1773f416;"

 

  private void httpPost() {

HttpConnection conn = null;OutputStream out =

null;

InputStream input = null;

int responseCode;

 

try {

System.out.println("URL: " + url);conn = (HttpConnection) Connector.open(

url);

conn.setRequestMethod(HttpConnection.POST);

 

conn.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded");

conn.setRequestProperty("User-Agent", "BlackBerry/4.5.0");conn.setRequestProperty(

"Content-Language", "en-US");

conn.setRequestProperty("Connection", "close");conn.setRequestProperty(

"Content-Length", Integer.toString(data.length()));

 

out = conn.openOutputStream();

out.write(
data.getBytes());

out.flush();

responseCode = conn.getResponseCode();

 

httpRespCode = responseCode;

if (responseCode != HttpConnection.HTTP_OK) {String ctype = conn.getHeaderField(

HEADER_CONTENTTYPE);

 

input = conn.openInputStream();

 

byte[] data = new byte[256];

int len = 0;

int size = 0;StringBuffer raw = new StringBuffer();

 

while ( -1 != (len = input.read(data)) )

{

raw.append(
new String(data, 0, len));

size += len;

}

 

raw.insert(0,
"bytes received]\n");

raw.insert(0, size);

raw.insert(0,
'[');

String content = raw.toString();

System.
out.println("RECEIVED STRING: " + content); if (input != null)

input.close();

if (conn != null)

conn.close();

stop();

 

....

...

For the password part I just encoded as base64 and made it as string format not byte stream.

Is anything wrong?

 

Thanks,

DK 

 

Please use plain text.
Administrator
MSohm
Posts: 14,064
Registered: ‎07-09-2008
My Carrier: Bell

Re: HttpConnection how to?

Can you elaborate on the behaviour you are seeing in your latest example?  Does it fail to send the data to the server, is not data read, is there an exception or is something else happening?
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
duksunkim
Posts: 141
Registered: ‎08-06-2008

Re: HttpConnection how to?

Sorry, nothing happened to server side. I supposed that server received request but didn't happen.

I don't see any log in server log file, either error or no error.

 

But I see something are going in and out through network sniffer but didn't log into server log.

It seems like that it 's not making http request.

 

 

Please use plain text.