【问题标题】:PostMethod HttpPost HttpClient - post xml with url parametersPostMethod HttpPost HttpClient - 使用 url 参数发布 xml
【发布时间】:2015-07-04 05:28:50
【问题描述】:

在 java 中有没有一种方法可以使用 PostMethod 或 HttpPost 以及 url 参数来发布 XML?我正在做类似下面的事情,但它不起作用。

URL - https://mytest.com?z=123&b=abc&c=%10

xml - <test>
        <data> This is test XML </data>
      </test>

public String getResponse(String xml) {

HttpClient client = new HttpClient();
// "https://mytest.com?z=123&b=abc&c=%10"
String url="https://mytest.com";
PostMethod pMethod = new pMethod(url);
pMethod.addParameter("z","123");
pMethod.addParameter("b","abc");
pMethod.addParameter("c","%10");
post.setRequestEntity(new StringRequestEntity(xml, "application/xml", "UTF-8"));
client.executeMethod(pMethod);
}

【问题讨论】:

    标签: java jakarta-ee http-post httpclient xml-rpc


    【解决方案1】:

    我建议您使用 REST 客户端来执行 HTTP POST。此外,您应该在 POST 的正文中传递您的 XML,而不是作为 URL 参数(URL 编码问题)。

    可以找到一个简单的 POST JSON 示例here

    【讨论】:

      【解决方案2】:

      我想我第一次误解了你的问题。您想将 XML 作为 URL 参数传递,但通过 POST 而不是直接在 URL 中包含 XML?你可以这样做:

      import java.net.URL;
      import java.net.HttpURLConnection;
      import java.net.URLEncoder;
      import java.io.BufferedWriter;
      import java.io.OutputStreamWriter;
      
      public class XMLPoster {
          public static void main(String[] argv) {
              try {
                  String XMLData = argv[0];
                  URL url = new URL("http://posttestserver.com/post.php");
                  String myParam = "myparam=" + URLEncoder.encode(XMLData, "UTF-8");
                  HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
                  httpConn.setRequestMethod("POST");
                  httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                  httpConn.setRequestProperty("Content-Length", Integer.toString(myParam.length()));
                  httpConn.setDoOutput(true);
      
                  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter((httpConn.getOutputStream())));
                  writer.write(myParam, 0, myParam.length());
                  writer.flush();
                  writer.close();
      
                  System.out.println(httpConn.getResponseCode());
              } catch (Exception e) {
                  e.printStackTrace();
              }
      
          }
      }
      

      如果你运行这个:

      $ java -cp . XMLPoster '<ThisXMLisFake>But how do you know?</ThisXMLisFake>'
      200
      

      并在 posttestserver.com 上找到相应的文件,它应该包含这个,希望这是你想要的:

      Post Params:
      key: 'myparam' value: '<ThisXMLisFake>But how do you know?</ThisXMLisFake>'
      Empty post body.
      
      Upload contains PUT data:
      myparam=%3CThisXMLisFake%3EBut+how+do+you+know%3F%3C%2FThisXMLisFake%3E
      

      【讨论】:

      • 我不想将 XML 作为参数传递。我有不同的参数要与 URL 一起发送,并且我需要发布 XML。
      【解决方案3】:

      你可以使用内置的 Java 东西来做到这一点:

      import java.net.URL;
      import java.net.HttpURLConnection;
      import java.io.BufferedWriter;
      import java.io.OutputStreamWriter;
      
      public class XMLPoster {
          public static void main(String[] argv) {
              try {
                  String XMLData = argv[0];
                  URL url = new URL("http://posttestserver.com/post.php");
                  HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
                  httpConn.setRequestMethod("POST");
                  httpConn.setRequestProperty("Content-Type", "application/xml; charset=utf-8");
                  httpConn.setRequestProperty("Content-Length", Integer.toString(XMLData.length()));
                  httpConn.setDoOutput(true);
      
                  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter((httpConn.getOutputStream())));
                  writer.write(XMLData, 0, XMLData.length());
                  writer.flush();
                  writer.close();
      
                  System.out.println(httpConn.getResponseCode());
              } catch (Exception e) {
                  e.printStackTrace();
              }
      
          }
      }
      

      这个代码当然只是一个例子,并没有做错误检查等。

      【讨论】:

      • 有没有办法将参数与 XML 一起传递给 URL?
      • 这不包括作为帖子一部分的参数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多