2011-01-24 34 views
1

我向我的AppEngine服務器發送POST請求。 HttpServletRequest的說我:AppEngine:如何查看來自POST請求的內容

POST /connexionDeconnexion HTTP/1.1 
User-Agent: Java/1.6.0_20 
Host: localhost:8888 
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 
Connection: keep-alive 
Content-Type: application/x-www-form-urlencoded 
Content-Length: 25 

我已經派了世界「HelloHelloHelloHelloHello」,根據內容長度是正確的。但是,我不知道如何恢復它。 你能解釋我嗎?

回答

4

由於Jigar說你可以使用request.getParameter()。如果您真的提交表單或將參數指定爲URL參數(http:// myhost/mypath?myparam = myvalue),則此方法有效。

如果您將數據作爲POST主體發送,則應從其主體讀取數據,即通過調用request.getInputStream()來檢索輸入流,然後從此流讀取數據。

1

你應該給出參數名和值,然後你可以從httpRequest對象中提取參數。

request.getParameter("paramName");

更新

客戶端

String param = "value"; 
File textFile = new File("/path/to/file.txt"); 
File binaryFile = new File("/path/to/file.bin"); 
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value. 

URLConnection connection = new URL(url).openConnection(); 
connection.setDoOutput(true); 
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 
PrintWriter writer = null; 
try { 
    OutputStream output = connection.getOutputStream(); 
    writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important! 

    // Send normal param. 
    writer.println("--" + boundary); 
    writer.println("Content-Disposition: form-data; name=\"param\""); 
    writer.println("Content-Type: text/plain; charset=" + charset); 
    writer.println(); 
    writer.println(param); 

    // Send text file. 
    writer.println("--" + boundary); 
    writer.println("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\""); 
    writer.println("Content-Type: text/plain; charset=" + charset); 
    writer.println(); 
    BufferedReader reader = null; 
    try { 
     reader = new BufferedReader(new InputStreamReader(new FileInputStream(textFile), charset)); 
     for (String line; (line = reader.readLine()) != null;) { 
      writer.println(line); 
     } 
    } finally { 
     if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {} 
    } 

    // Send binary file. 
    writer.println("--" + boundary); 
    writer.println("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\""); 
    writer.println("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()); 
    writer.println("Content-Transfer-Encoding: binary"); 
    writer.println(); 
    InputStream input = null; 
    try { 
     input = new FileInputStream(binaryFile); 
     byte[] buffer = new byte[1024]; 
     for (int length = 0; (length = input.read(buffer)) > 0;) { 
      output.write(buffer, 0, length); 
     } 
     output.flush(); // Important! Output cannot be closed. Close of writer will close output as well. 
    } finally { 
     if (input != null) try { input.close(); } catch (IOException logOrIgnore) {} 
    } 
    writer.println(); // Important! Indicates end of binary boundary. 

    // End of multipart/form-data. 
    writer.println("--" + boundary + "--"); 
} finally { 
    if (writer != null) writer.close(); 
} 

請參見

+0

感謝您的回答。 如果我發送了此字符串: writer = new OutputStreamWriter(conn.getOutputStream()); \t \t writer.write(「HelloHelloHelloHelloHello」); 什麼是參數? 使用調試模式時,_parameters字段爲空。這是正常的嗎? – g123k 2011-01-24 09:44:38

+0

檢查更新.............. – 2011-01-24 09:48:17