PostMethod:特定の住所にリクエストを行う方法は?
-
19-09-2019 - |
質問
特定のアドレスへの投稿リクエストを繰り返したいと思います。たとえば、
投稿リクエストのために、私は普遍的な方法を作成しました:
private String postMethod(String url, HashMap<String, String> headers, String encodedAuthorizationString) throws HttpException, IOException {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
post.setRequestHeader("Authorization", encodedAuthorizationString);
if(headers != null && !headers.isEmpty()){
for(Entry<String, String> entry : headers.entrySet()){
post.setRequestHeader(new Header(entry.getKey(), entry.getValue()));
}
}
client.executeMethod(post);
String responseFromPost = post.getResponseBodyAsString();
post.releaseConnection();
return responseFromPost;
}
ここで、ヘッダーはペア(キー、値)を表します。 PostMethod( ")を呼び出してメソッドを使用しようとしました。http://staging.myproject.com.products.xml"、ヘッダー、" xxx ");ヘッダーにはペアが含まれています
(「製品[タイトル]」、「Titletest」)、
(「製品[コンテンツ]」、「テストコンテンツ」)、
(製品[価格]、 "12.3")、
(「タグ」、「AAA、BBB」)
しかし、サーバーはエラーメッセージを返しました。
アドレスを解析する方法を知っている人はいますか?
上記の方法でそれを使用するために? URLはどの部分ですか?パラメーターは正しく設定されていますか?
ありがとうございました。
解決
私は問題を見つけました:
private String postMethod(String url, HashMap<String, String> headers, String encodedAuthorizationString) throws HttpException, IOException {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
post.setRequestHeader("Authorization", encodedAuthorizationString);
if(headers != null && !headers.isEmpty()){
for(Entry<String, String> entry : headers.entrySet()){
//post.setRequestHeader(new Header(entry.getKey(), entry.getValue()));
//in the old code parameters were set as headers (the line above is replaced with the line below)
post.addParameter(new Header(entry.getKey(), entry.getValue()));
}
}
client.executeMethod(post);
String responseFromPost = post.getResponseBodyAsString();
post.releaseConnection();
return responseFromPost;
}
パラメーター:
(「製品[タイトル]」、「Titletest」)、
("product[content]", "TestContent"), (product[price], "12.3"), ("tags", "aaa,bbb")
他のヒント
HTTPリクエストヘッダーを使用した製品[価格] = 12.3など、URLクエリパラメーターを混乱させているようです。 SetRequestHeader()を使用すると、HTTPリクエストに関連付けられたメタデータであるHTTPリクエストヘッダーを設定することを目的としています。
クエリパラメーターを設定するには、「?」の後、それらをURLに追加する必要があります。 URLの例のように、Urlencoded。
所属していません StackOverflow