03-18-2009 03:17 PM - edited 03-18-2009 03:29 PM
Hi!
I'm trying to send HTTP POST request on a simulator using JDE 4.6.0.
When data length is less then 6200 bytes everything works fine but when i try to send 6300 bytes simulator throws timeout exception after 2 minutes waiting.
Does anyone know why this is happening?
Sample code:
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class HTTP {
public static void testPost() {
String url = "http://some_test_url";
StringBuffer sb = new StringBuffer(6300);
for (int i = 0; i < 6300; i++) {
sb.append('A');
}
try {
String result = post(url, sb.toString().getBytes());
}
catch (Exception e) {
}
}
public static String post(String url, byte[] data) throws Exception {
try {
InputStream is;
HttpConnection http = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setRequestProperty("Content-length", "" + data.length);
OutputStream out = http.openOutputStream();
out.write(data);
out.flush();
String response = null;
byte[] buff;
int rc = http.getResponseCode();
if (rc == HttpConnection.HTTP_OK) {
int len = (int) http.getLength();
is = http.openInputStream();
if (len != -1) {
buff = new byte[len];
is.read(buff);
}
else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
baos.write(ch);
}
buff = baos.toByteArray();
baos.close();
}
response = new String(buff);
}
else {
throw new IOException("HTTP response code: " + rc);
}
http.close();
return response;
}
catch (Exception e) {
throw new Exception("Can't send http post request" );
}
}
}
03-18-2009 06:37 PM
I researched the issue further and discovered that there is no such problem with JDE 4.7.0.
I assume the cause is in MDS-CS Simulator which I use to simulate internet connection.
So the question how to resolve this iin JDE 4.6.0 is still open and any help is welcome.
08-13-2009 09:18 AM
Did you ever get this resolved with 4.6.0? I'm having the same issue. Also, does the problem translate to the actual devices, or does it just affect the phone/mds simulator?
Thanks,
Parks
08-13-2009 11:18 AM
05-13-2010 05:58 AM
OutputStream out = http.openOutputStream();
after flush just close it
out.flush();
out.close();