我想reate POST請求到指定的地址,讓它例如是PostMethod:如何向給定地址發送請求?
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;
}
其中標題代表成對(鍵,值),例如(「product [title]」,「TitleTest」)。我試圖通過調用 postMethod(「http://staging.myproject.com.products.xml」,標題,「xxx」); 其中報頭包括對
( 「產物[標題]」, 「TitleTest」),
( 「產物[內容]」, 「TestContent」),
(產物[價格] , 「12.3」),
( 「標籤」, 「AAA,BBB」)
但服務器返回一個錯誤消息。
有誰知道如何以上述的方法使用它解析地址
?哪部分是網址?參數設置是否正確?
謝謝。