使用 HttpClient 写入正文请求

2024-01-10

我想用 XML 内容类型编写请求正文,但我不知道如何使用 HttpClient 对象(http://hc.apache.org/httpclient-3.x/apidocs/index.html http://hc.apache.org/httpclient-3.x/apidocs/index.html )

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(this.url);
httpRequest.setHeader("Content-Type", "application/xml");

我不知道如何继续用我的 XML 编写正文......


如果你的xml是这样写的java.lang.String你可以只使用HttpClient这样

    public void post() throws Exception{
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://www.baidu.com");
        String xml = "<xml>xxxx</xml>";
        HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        String result = EntityUtils.toString(response.getEntity());
    }

注意例外情况。

顺便说一句,该示例是由 httpclient 版本 4.x 编写的

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 HttpClient 写入正文请求 的相关文章

随机推荐