簡體   English   中英

如何使用HttpClient發送發帖請求

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM