[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cannot get J2ME HTTP POST to work

Posted on 2006-03-30
2
Medium Priority
?
546 Views
Last Modified: 2008-02-01
Hi,

Can someone tell me why this code is not working. I am using JBuilder2006 with integrated Sun wireless toolkit, the latest version I believe. I am using a simple Tomcat JSP page to read the POST and send a response.  

GET operations work fine. But on the POST, the jsp recieves the request but the post body is not making it through. The jsp does report that the request type as post. The username and password parameters are null within the JSP.

Please help!

HttpConnection c = (HttpConnection)Connector.open("http://localhost:8080/WebModule2/jsp1.jsp",Connector.READ_WRITE);
String content = "username=beta&password=tester\r\n";
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream os = c.openOutputStream();
os.write(content.getBytes());
os.flush();
os.close();
   
StringBuffer sb = new StringBuffer();
InputStream is = c.openDataInputStream();
byte b[] = new byte[1024];
int cnt = 0;
long ts = System.currentTimeMillis();
while ( cnt > 0 || System.currentTimeMillis()-ts < 3000)
{
   cnt = is.read(b,0,b.length);
   if(cnt>0)
   {
     sb.append(new String(b,0,cnt));
     ts = System.currentTimeMillis();
   }
}
 response = sb.toString();
0
Comment
Question by:dgbweb
2 Comments
 
LVL 13

Accepted Solution

by:
bglodde earned 2000 total points
ID: 16334786
With a GET request, you don't have to send extra info, but with a POST, you have some extra requirements.
I'm assuming you are already launching this in it's own Thread, outside of the main thread, since your GETs are working.
Let's try setting the content-length of the POST first...

String clen = Integer.toString(content.length());
c.setRequestProperty("Content-length", clen);

See if that allows the body to return.
0
 
LVL 3

Author Comment

by:dgbweb
ID: 16339662
that fixed it

Thanks.

I have another question (see separate post for more points :-)

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a step by step guide on how to create a basic PTP link using Ubiquiti airOS devices. This guide can be used on the following Ubiquiti AirMAX devices. Nanostation, Bullets, AirBridge, Nanobeam, NanoBridge to name a few. Please review …
Today sees the launch of a new case study, focusing on BYOD technologies we have been working with for some time now.  But with the advent of 802.11ac wireless technologies and the story behind our landmark developments, we would like to share this …
This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question