How to send XML POST request using Apache HttpClient?

我的梦境 提交于 2019-12-23 15:25:21

问题


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>

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);

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

username=stackoverflow&password=12345

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

Thanks in advance.


回答1:


Use a different kind of HttpEntity. There are a number of implementations listed at the top of the documentation.



来源:https://stackoverflow.com/questions/9848557/how-to-send-xml-post-request-using-apache-httpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!