使用 HttpClient 写入正文请求 [英] Write in body request with HttpClient

查看:32
本文介绍了使用 HttpClient 写入正文请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to write the body of a request with XML content-type but I don't know how with HttpClient Object ( 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 编写正文...

And I don't know how to continue to write the body with my XML ...

推荐答案

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

If your xml is written by java.lang.String you can just using HttpClient in this way

    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 版本编写的

BTW, the example is written by the httpclient version 4.x

这篇关于使用 HttpClient 写入正文请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆