2010-01-20 106 views
1

我正在爲來自j2me應用程序的CDC發出HTTP請求。 GET請求方法鐵鍋就好了,但是當我使用POST方法我得到的消息:來自j2me的POST HTTP請求

狀態線路代碼: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*/ 
     } 
    } 
    } 

回答

2

刪除此行

connection.setRequestProperty("Content-length", String.valueOf(msg.getBytes().length)); 

並再次嘗試你的代碼。

+0

此解決方案工作。 謝謝! – Max 2010-01-21 12:10:57