简体   繁体   English

如何使用 Apache HttpClient 发送 XML POST 请求?

[英]How to send XML POST request using Apache HttpClient?

I want to do a HTTP POST of the format as follows,我想做如下格式的 HTTP POST,

<?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,对于任何基于登录的 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,但是通过这种方式,POST 数据将如下所示,

username=stackoverflow&password=12345

How can I format this request as per the specified XML format that I mentioned above?如何根据我上面提到的指定 XML 格式格式化此请求?

Thanks in advance.提前致谢。

Use a different kind of HttpEntity .使用不同类型的HttpEntity There are a number of implementations listed at the top of the documentation . 文档顶部列出了许多实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "如何使用 Apache HttpClient 发布 JSON 请求?" - How to POST JSON request using Apache HttpClient? 如何使用Apache HttpClient 4.5在big5中发送发布请求并获得响应 - how to send post request and get response in big5 using Apache HttpClient 4.5 如何使用HttpClient发送发帖请求 - How to send a post request with HttpClient 如何使用 Java Apache HttpClient 正确发出 POST 请求? - how to properly make a POST request using Java Apache HttpClient? 如何使用Apache HttpClient发布NON-JSON请求? - How to POST NON-JSON request using Apache HttpClient? 如何使用 Apache HttpClient 在 Post 请求中编码俄语文本? - How to encode russian text in Post request using Apache HttpClient? 如何使用 Apache HTTPClient 4.5 发送带有 JSON 参数的 POST。 获取 415 不受支持的媒体类型 - How to send a POST with JSON parameters using Apache HTTPClient 4.5. Getting 415 unsupported media type 如何使用httpclient发送URL帖子 - how to send the url post using httpclient 如何使用Apache HTTPClient的post方法将列表发送到服务器 - How to use Apache HTTPClient's post method to send a List to the server 如何使用HttpURLConnection以与Apache HttpClient lib相同的方式发出POST请求 - How can I make a POST request in the same manner as Apache HttpClient lib, using HttpURLConnection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM