2013-04-06 20 views
2

我想在應用程序引擎應用程序下使用urlfetch做POST請求。HttpURLConnection應用程序引擎POST請求的Java示例不起作用

我遵循從App Engine文檔(此處爲https://developers.google.com/appengine/docs/java/urlfetch/usingjavanet)中找到的簡單示例中提取的說明(和代碼),位於「使用HttpURLConnection」一節下。

import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 

    String message = URLEncoder.encode("my message", "UTF-8"); 

    try { 
     URL url = new URL("http://httpbin.org/post"); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoOutput(true); 
     connection.setRequestMethod("POST"); 

     OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); 
     writer.write("message=" + message); 
     writer.close(); 

     if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
      // OK 
     } else { 
      // Server returned HTTP error code. 
     } 
    } catch (MalformedURLException e) { 
     // ... 
    } catch (IOException e) { 
     // ... 
    } 

爲了測試此POST請求,我正在使用以下網站「http://httpbin.org/post」。

獲取和連接工作 - 但是,連接發送爲GET而不是POST。

下面是我從這個請求得到了響應:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> 
<title>405 Method Not Allowed</title> 
<h1>Method Not Allowed</h1><p>The method GET is not allowed for the requested URL.</p> 

有任何人碰到這個問題?

任何幫助表示讚賞。

+0

這示例代碼正常工作對我來說,我到'// OK'部分 – 2013-04-06 21:45:09

+0

我也一樣 - 但是,迴應說405:方法不允許。這意味着HttpURLConnection發送GET而不是POST。我想知道它爲什麼發送GET而不是POST請求。 – user1220958 2013-04-07 00:33:42

+0

我仍然不知道爲什麼App Engine文檔中的這個示例不起作用。我最終使用了此處介紹的方法([link](http://stackoverflow.com/a/13468601/1220958))能夠執行POST請求。 – user1220958 2013-04-07 03:11:34

回答

0

您是否嘗試過調用OutputStreamWriter的flush()方法?

+0

它關閉時沖洗。大多數流都是。 – JIV 2016-10-10 12:28:24

0

也許你必須設置內容類型請求屬性:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
3

由於這個問題仍然得到了相當多的意見3年後,我會在這裏確認method given in the documentation sample工作正常進行POST請求到給定的URL,並且自這個問題首次發佈以來一直保持不變。工作代碼示例如下:

String message = URLEncoder.encode("my message", "UTF-8"); 

URL url = new URL("http://httpbin.org/post"); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.setDoOutput(true); 
conn.setRequestMethod("POST"); 

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); 
writer.write("message=" + message); 
writer.close(); 

StringBuffer responseString = new StringBuffer(); 
String line; 

BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
while ((line = reader.readLine()) != null) { 
    responseString.append(line); 
} 
reader.close(); 

下收到答覆:

Code: 200, message: { "args": {}, "data": "", "files": {}, "form": { 
"message": "my message" }, "headers": { "Accept-Encoding": 
"gzip,deflate,br", "Content-Length": "18", "Content-Type": 
"application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": 
"AppEngine-Google; (+http://code.google.com/appengine; appid: s~ 
<redacted>)", "X-Cloud-Trace-Context": ",<redacted>" }, "json": null, 
"origin": "107.178.194.113", "url": "http://httpbin.org/post"}