Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpConnection and POST

 
Ould Nadif
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Je voudrai transcricre en Java (J2ME) ceci:
I would like write in Java(J2ME) these below:

POST /sendsms HTTP/1.1
Host: www.myhost.com:80
Content-Type: application/x-www-form-urlencoded; charset="utf-8"
Content-Length: 81

?Username=ANOther.12345&PIN=9292&SendTo=447700912345&Message=Testtest



Here are my code java but it doesn't work and I don't know why:


Thread t = new Thread() {

public void run() {

String url = "www.myhost.com/sendsms";
HttpConnection http = null;

try{

http = (HttpConnection)Connector.open(url);
http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=\"utf-8\"");
String data = "?UN=ANOther.12345&PIN=9292&SendTo=447700912345&Message=Testtest";

String Taille = "" + data.length();
http.setRequestProperty("Content-Length",Taille );

OutputStream oStrm =null;
oStrm = http.openOutputStream();
oStrm.write(data.getBytes());

oStrm.flush();
oStrm.close();

}catch(Exception ee){
System.out.println("Pnb ");

}
 
James Reilly
wrangler
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't provide a lot of information about what the problem is.

One approach might be to start with a simple known working MIDlet/servlet
pair that use HTTP (there's lots of those available) and testing that both of those work in whatever J2ME device/SDK and server, network configuration, etc. that you are using. So you have a known working starting point.

And then modify that MIDlet and servlet pair as appropriate: e.g. setting the Content-Type, Content-Length, URL, POST contents, etc. on both sides (MIDlet and servlet) as appropriate.
[ July 09, 2004: Message edited by: James Reilly ]
 
Ould Nadif
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.

I have not resolved my problem. I received a code error HTTP 411.

Here my code source:

String url = "http://www.myhost.com/sendsms";
HttpConnection http = null;

try{

http = (HttpConnection)Connector.open(url,Connector.READ_WRITE);
http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=\"utf-8\"");
String data = "UN=ANOther.12345&PIN=9292&SendTo=447700912345&Message=Testtest";

String Taille = "" + data.length();
http.setRequestProperty("Content-Length",Taille );
OutputStream oStrm = http.openOutputStream();
oStrm.write(data.getBytes());
oStrm.flush();

System.out.println("Resultat " + http.getResponseCode() ) ;

oStrm.close();


}catch(Exception e)
{
e.printStackTrace();
System.out.println("erreur : "+e.getMessage());
}

}catch(Exception ee){
System.out.println("Pnb ");

}


OULD NADIF
 
dan moore
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,

a couple of possibilities:

(1) error code 411 means the server has not received the 'Content-Length' header, which could be an intervening gateway stripping this out...

(2) your code is also calling http.getResponseCode() before it closes the output stream - maybe try closing the output stream before doing the getResponseCode().

(3) the flush() shouldn't be necessary... i believe i have seen it cause problems on some devices, too.

hope that helps,
dan.
 
dan moore
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...or maybe try not setting 'Content-Length' explicitly - some implementations do this implicitly, maybe it's interfering.

cheers, dan.
 
Ould Nadif
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.

I tried your idea I modified my code.It's better but I have a exception.

Here my code source:

String url = "http://www.myhost.com/sendsms";
HttpConnection http = null;

try{

http = (HttpConnection)Connector.open(url,Connector.READ_WRITE);
http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=\"utf-8\"");
String data = "UN=ANOther.12345&PIN=9292&SendTo=447700912345&Message=Testtest";
System.out.println("Resultat " + http.getResponseCode() ) ;
if(http.getResponseCode() == HttpConnection.HTTP_OK){
oStrm = http.openOutputStream();
oStrm.write(data.getBytes());
oStrm.flush();
oStrm.close();
http.close();
}

}catch(Exception e)
{
e.printStackTrace();
System.out.println("erreur : "+e.getMessage());
}

}catch(Exception ee){
System.out.println("Pnb ");

}



Here the result:

Resultat 200

java.lang.IllegalStateException: Write attempted after request finished

at com.sun.midp.io.j2me.http.Protocol.writeBytes(+16)

at com.sun.midp.io.BaseOutputStream.write(+52)

at java.io.OutputStream.write(+8)

at KeyEventsCanvas$1.run(+104)



OULD NADIF
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic