我正在为CDC的j2me应用程序发出HTTP请求。 GET请求方法很好,但是当我使用post方法时,我收到消息:
状态行代码:413 状态行消息:请求实体太大
我发送的消息只有5个字符,所以我不知道哪个是问题。
代码列在下面。
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*/
}
}
}
答案 0 :(得分:2)
删除此行
connection.setRequestProperty("Content-length", String.valueOf(msg.getBytes().length));
再次尝试使用您的代码。