POST HTTP request from j2me
I am making an HTTP request from a j2me application for CDC. The GET request method woks just fine but when I am using the post method I get the message:
Status Line Code: 413 Status Line Message: Request Entity Too Large
The message that I am sending is only 5 characters long so I don't know which is the problem.
The code is listed bellow.
HttpConnection connection = null;
InputStream inputstream = null;
try
{
connection = (HttpConnection) Connector.open(someURL);
//HTTP Request
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
connection.setRequestProperty("Content-Language", "zh-tw");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
if (cookie != null){
connection.setRequestProperty("cookie", cookie);
}
String msg = "u=123";
connection.setRequestProperty("Content-length", String.valueOf(msg.getBytes().length));
System.out.println(msg.getBytes().length);
OutputStream out = connection.openOutputStream();
out.write(msg.getBytes());
out.flush();
// HTTP Response
System.out.println("Status Line Code: " + connection.getResponseCode());
System.out.println("Status Line Message: 开发者_开发知识库" + connection.getResponseMessage());
if (connection.getResponseCode() == HttpConnection.HTTP_OK)
{
//some code
}
}
catch(IOException error)
{
/*log error*/
}
finally
{
if (inputstream!= null)
{
try
{
inputstream.close();
}
catch( Exception error)
{
/*log error*/
}
}
if (connection != null)
{
try
{
connection.close();
}
catch( Exception error)
{
/*log error*/
}
}
}
remove this line
connection.setRequestProperty("Content-length", String.valueOf(msg.getBytes().length));
and try your code again.
精彩评论