2

指定されたアドレスにPOSTリクエストを再送信したいのですが、たとえば

http://staging.myproject.com/products.xml?product[title]=TitleTest&product[content]=TestContent&product[price]=12.3&tags=aaa,bbb

POSTリクエストの場合、ユニバーサルメソッドを作成しました。

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;
}

ここで、headersはペア(key、value)を表します(例: "product [title]"、 "TitleTest")。postMethod( " http://staging.myproject.com.products.xml "、headers、 "xxx");を呼び出してメソッドを使用しようとしました。ヘッダーにペアが含まれている場合

("product [title]"、 "TitleTest")、

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

(product [price]、 "12.3")、

("タグ"、 "aaa、bbb")

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

誰かがアドレスを解析する方法を知っていますか

http://staging.myproject.com/products.xml?product[title]=TitleTest&product[content]=TestContent&product[price]=12.3&tags=aaa,bbb

上記の方法で使用するには?URLはどの部分ですか?パラメータは正しく設定されていますか?

ありがとうございました。

4

2 に答える 2

3

問題が見つかりました:

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

パラメーター:

("商品[タイトル]", "タイトルテスト"),

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

(product[price], "12.3"),

("tags", "aaa,bbb")
于 2010-01-08T13:32:31.543 に答える
1

product[price]=12.3 などの URL クエリ パラメータと HTTP リクエスト ヘッダーを混同しているようです。setRequestHeader() を使用することは、HTTP 要求ヘッダーを設定することを意味します。これは、HTTP 要求に関連付けられたメタデータです。

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

于 2010-01-08T13:27:28.963 に答える