質問

特定のアドレスへの投稿リクエストを繰り返したいと思います。たとえば、

http://staging.myproject.com/products.xml?product [Title] = titletest&product [Content] = testcontent&product [= 2.3&tags=aaa、bbb

投稿リクエストのために、私は普遍的な方法を作成しました:

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」)

しかし、サーバーはエラーメッセージを返しました。

アドレスを解析する方法を知っている人はいますか?

http://staging.myproject.com/products.xml?product [Title] = titletest&product [Content] = testcontent&product [= 2.3&tags=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;
}

url = http://staging.myproject.com/products.xml

パラメーター:

(「製品[タイトル]」、「Titletest」)、

("product[content]", "TestContent"),

(product[price], "12.3"),

("tags", "aaa,bbb")

他のヒント

HTTPリクエストヘッダーを使用した製品[価格] = 12.3など、URLクエリパラメーターを混乱させているようです。 SetRequestHeader()を使用すると、HTTPリクエストに関連付けられたメタデータであるHTTPリクエストヘッダーを設定することを目的としています。

クエリパラメーターを設定するには、「?」の後、それらをURLに追加する必要があります。 URLの例のように、Urlencoded。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top