1

コードは次のとおりです。

String Surl = "http://mysite.com/somefile";
String charset = "UTF-8";
query = String.format("param1=%s&param2=%s",
URLEncoder.encode("param1", charset),
URLEncoder.encode("param2", charset));

HttpURLConnection urlConnection = (HttpURLConnection) new URL(Surl + "?" + query).openConnection();
urlConnection.setRequestMethod("POST");             
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setAllowUserInteraction(false);
urlConnection.setRequestProperty("Accept-Charset", charset);
urlConnection.setRequestProperty("User-Agent","<em>Android</em>");
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=" + charset);                
urlConnection.connect();

上記はまだGETリクエストを行います。 サーバーで PHP を使用しており、2.3.7 (デバイス) でテスト済み$_GETの変数ではなく、変数を$_POST介してクエリの 'name=value' パラメータにアクセスできます。

何が欠けていますか?

4

1 に答える 1

3

URL でパラメーターを送信すると、それらは GET 変数に入れられます。探しているものを実現するには、リクエストの POST 本文にパラメーターを投稿する必要があります。connect() 呼び出しの直前に次を追加し、「?」を削除する必要があります。+ URL からのクエリ。

    urlConnection.setRequestProperty("Content-Length", String.valueOf(query.getBytes().length));            
    urlConnection.setFixedLengthStreamingMode(query.getBytes().length);

    OutputStream output = new BufferedOutputStream(urlConnection.getOutputStream());            
    output.write(query.getBytes());
    output.flush(); 
    output.close();
于 2012-09-27T20:29:11.310 に答える