0

これは標準的なものかもしれませんが、うまく動作しません。org.apache.commons.httpclient.methodsJava コードから Http リクエストを作成するために使用しています。ある例では、PUT リクエストを作成していくつかのパラメーターを渡す必要がありました。私は次の方法でそれをやっています:

PutMethod putMethod = new PutMethod(url);
putMethod.getParams().setParameter("param1", "param1Value");
putMethod.getParams().setParameter("param2", "param2Value");
httpClient.executeMethod(putMethod);

しかし、サーバーでこれらのパラメーターを読み取ろうとすると、取得できるのはnull.

ただし、動作するように変更するurlurl?param1=param1Value&param2=param2Value

setParameter メソッドを使用して動作させるにはどうすればよいですか?

4

2 に答える 2

1

Query Params を PutMethod に追加するには、次の方法に従います。

    NameValuePair[] putParameters = new NameValuePair[2];   
    putParameters[0] = new NameValuePair(Param1, value1);       
    putParameters[1] = new NameValuePair(Param2, value2);   


    HttpClient client = new HttpClient();
    PutMethod putMethod = new PutMethod(url);       

    putMethod.setQueryString(putParameters);

それから電話して、

int response = client.executeMethod(putMethod);

代わりにputMethod.setQueryString(putParameters);使用することもできます

putMethod.setRequestBody(EncodingUtil.formUrlEncode(putParameters, "UTF-8")); (これは非推奨です)

GetMethod、PostMethod は、上記のコードと比較して、Query Params を追加するときにわずかな違いがあります。

その他のコード例: http://www.massapi.com/class/pu/PutMethod.html

お役に立てれば。

于 2014-10-21T14:42:11.347 に答える
-1

サーバー側のコードはPUTメソッドをサポートする必要があります

たとえば、サーブレットの場合、メソッドを含めることができます

doPUT(); // your put request will be delivered to this method

ジャージなどのRESTベースのフレームワークを使用する場合

あなたが使用することができます

@PUT 
   Response yourPutMethod(){..}
于 2013-01-28T09:14:04.963 に答える