如何使用Apache HttpClient发送XML POST请求? [英] How to send XML POST request using Apache HttpClient?

查看:423
本文介绍了如何使用Apache HttpClient发送XML POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照以下格式进行HTTP POST:

I want to do a HTTP POST of the format as follows,

<?xml version="1.0" encoding="UTF-8" ?>
<authRequest>
 <username>someusernamehere</username>
 <password>somepasswordhere</password>
</authRequest>

我通常使用以下机制进行任何基于登录的POST,

I usually work with the following mechanism for any login based POST,

HttpParams params = new BasicHttpParams();
        params.setParameter(
                "http.useragent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6");
        DefaultHttpClient httpclient = new DefaultHttpClient(params);

        HttpPost httppost = new HttpPost("http://mysite.com/login");
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("username", "stackoverflow"));
        formparams.add(new BasicNameValuePair("password", "12345"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
        httppost.setEntity(entity);
        HttpResponse httpresponse = httpclient.execute(httppost);

但是通过这种方式,POST数据看起来像,

But with this way, the POST data will be look like,

username=stackoverflow&password=12345

如何根据我上面提到的指定XML格式格式化此请求?

How can I format this request as per the specified XML format that I mentioned above?

提前致谢。

推荐答案

使用其他类型的 HttpEntity 文档

这篇关于如何使用Apache HttpClient发送XML POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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