problem in http post request
my code is as follows
[CODE]
public String sentPostCommentToServer(String uploadURL, String data) {
System.out.println("contact upload url " + uploadURL);
System.out.println("contact upload data " + data);
//Log.setLogLevel(Log.INFO);
//Log.info(LOG_TAG, "url to hit=" + uploadURL);
// Log.info(LOG_TAG, "data to post=" + data);
HttpConnection hc = null;
InputStream is = null;
StringBuffer stb = new StringBuffer();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
hc = (HttpConnection) Connector.open(uploadURL);
hc.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=utf-8");
hc.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Confirguration/CLDC-1.0");
hc.setRequestProperty("Content-Language", "en-US");
hc.setRequestProperty("Content-Length", "" + data.length());
hc.setRequestMethod(HttpConnection.POST);
OutputStream dout = hc.openOutputStream();
byte[] Data = data.getBytes();
System.gc();
dout.write(Data);
dout.flush();
//dout.close();
System.out.println("after flush");
//System.out.println("value of getresponse code"+hc.getResponseCode());
int ch;
is = hc.openInputStream();
System.out.println("after openInputstream");
long len = hc.getLength();
System.out.println("content length = " + len);
if (len != -1) {
// Read exactly Content-Length bytes
for (int i = 0; i < len; i++)
if ((ch = is.read()) != -1) {
stb.append((char) ch);
}
} else {
// Read until the connection is closed.
while ((ch = is.read()) != -1) {
len = is.available();
stb.append((char) ch);
}
}
System.out.println(stb.toString());
if (hc.getResponseCode() == 200) {
System.out.println("value of getresponse code"+hc.getResponseCode());
}
if (hc.getResponseCode() != 200) {
}
String temp = stb.toString();
if (temp.length() == 0) {
temp = "zero length";
}
// Log.setLogLevel(Log.INFO);
//Log.info(LOG_TAG, "responce of this post=" + stb.toString());
return stb.toString();
} catch (ConnectionNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// close all the connections.
try {
if (bos != null)
bos.close();
if (is != null)
is.close();
if (hc != null)
hc.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return "Connection Not Found";
}
[/CODE]
there is no response from server
Re: problem in http post request
Is the server up & running - are you able to reach it directly from browser?
In the flow of above code-- till where you are able to reach -- what the values printed by SOP's --- System.out.println()'s?
Re: problem in http post request
sandeepintg, where you able to resolve your issue? Did you try to run this from within one of the [url=http://www.developer.nokia.com/Develop/Java/Tools/Series_40_platform_SDKs/]Series 40 Platform SDKs[/url]? With those, you are able to run several diagnosis windows for network traffic. You can also use Wireshark, to make absolutely sure. If this happens only on a real device, using a WLAN enabled Series 40 helped me a lot to understand the various bugs (mine and Nokia’s). With WLAN, you can capture the traffic (either by placing a protocol sniffer on the Ethernet behind the WLAN access point or by doing a air sniff).