2

アプリ エンジン アプリケーションで urlfetch を使用して POST リクエストを実行しようとしています。

App Engine のドキュメント (ここではhttps://developers.google.com/appengine/docs/java/urlfetch/usingjavanet ) の「Using 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 リクエストをテストするために、次の Web サイト " http://httpbin.org/post " を使用しています。

フェッチと接続は機能しますが、接続は POST ではなく GET として送信されます。

このリクエストに対するレスポンスは次のとおりです。

<!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>

誰かがこの問題に遭遇しましたか?

どんな助けでも大歓迎です。

4

3 に答える 3

0

Maybe you must set content-type request property:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
于 2014-09-17T15:05:29.377 に答える
0

OutputStreamWriter の flush() メソッドを呼び出してみましたか?

于 2013-08-21T18:30:24.640 に答える