4

我想做一个格式如下的HTTP POST,

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

对于任何基于登录的 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 数据看起来像,

username=stackoverflow&password=12345

如何按照我上面提到的指定 XML 格式来格式化这个请求?

提前致谢。

4

1 回答 1

5

使用不同类型的HttpEntity. 文档顶部列出了许多实现。

于 2012-03-24T11:27:00.200 回答