使用HttpClient写入正文请求

编程入门 行业动态 更新时间:2024-10-26 18:22:38
本文介绍了使用HttpClient写入正文请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用XML内容类型编写请求的主体,但我不知道如何使用HttpClient对象(hc.apache/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 ( hc.apache/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("www.baidu"); 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写入正文请求

本文发布于:2023-11-14 11:26:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587085.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正文   HttpClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!