httpclient在java中发布xml内容

问题描述 投票:0回答:3

我正在尝试使用 httpclient 发布 xml 请求,如下所示:

String parm1 = MyXml.toString();
PostMethod post = new Postmethod(url);
post.setRequestEntity(new StringRequestEntity(parm1));
...

我的程序中有一个对象,我想将其转换为 xml 表示形式。

我的问题是,在java中以xml格式创建Myxml的最佳方法是什么,然后我可以稍后打印出它的字符串格式。

谢谢。

java xml apache-commons-httpclient
3个回答
0
投票

在 Java 中创建 XML 有很多选项。这个答案如何使用 JAXB 序列化和反序列化对象?提供了一种似乎适合您的用例的常见方法的良好演示。


0
投票

以下是如何使用 Apache HttpClient 发布 xml 请求。

  • 使用apache Velocity创建请求xml格式
  • 使用 Castor 将响应流( respReader )转换为 java 对象

    final String request = createXmlRequest(); // helper method to create the xml request
    final HttpClient client = new HttpClient();
    final PostMethod post = new PostMehod(url); // url - www.google.cm/someoperaion
    
    post.setRequestHeader("Content-Language", "en-US");
    post.setRequestEntity(new StringRequestEntity(request, "text/xml", "ISO-8859-1"));
    
    final int returnCode = client.executeMethod(post);
    
    final BufferedReader respReader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
    

-1
投票

尝试这样使用它...

public void postData() throws Exception {


 HttpClient client = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("https://www.xyz.com");

 List<NameValuePair> list = new ArrayList<NameValuePair>(1);

 list.add(new BasicNameValuePair("name","ABC");

 httppost.setEntity(new UrlEncodedFormEntity(list));

 HttpResponse r = client.execute(httppost);

}
© www.soinside.com 2019 - 2024. All rights reserved.