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

查看:235
本文介绍了使用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());
    }

注意例外情况。

BTW,示例由httpclient版本4.x

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

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

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