![](/img/trans.png)
[英]Java httpclient 4.1 how to send post request with configurable port
[英]How to send a post request with HttpClient
有誰知道我將如何使用帶有HttpClient庫的Java Eclipse中的發布請求來更新頁面? 當前,這就是我所擁有的,但是當我執行它時,我得到一個頁面未找到錯誤:
public void update() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://examplepage.xml");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("_action", "<BasicPage><title>New Title</title></BasicPage>"));
nameValuePairs.add(new BasicNameValuePair("_method", "post"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String info = (""+EntityUtils.toString(entity));
System.out.println(info);
System.out.println(response.getEntity().getContent());
System.out.println(response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
您的代碼看起來不錯。 檢查頁面URI或別名“ examplepage.xml”是否確實存在或映射。 還要檢查它是否可以接受POST請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.