简体   繁体   English

如何使用HttpClient发送发帖请求

[英]How to send a post request with HttpClient

Does anyone know how I would go about updating a page using a post request in Java Eclipse with the HttpClient library? 有谁知道我将如何使用带有HttpClient库的Java Eclipse中的发布请求来更新页面? Currently this is what I have, but when I execute it I get a page not found error: 当前,这就是我所拥有的,但是当我执行它时,我得到一个页面未找到错误:

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

Your code looks fine. 您的代码看起来不错。 Check that the page URI or alias "examplepage.xml" actually exists or mapped. 检查页面URI或别名“ examplepage.xml”是否确实存在或映射。 Check also that it can accept POST requests. 还要检查它是否可以接受POST请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM