【问题标题】:I need to send a POST request to an api using an XML request structure in java我需要使用 java 中的 XML 请求结构向 api 发送 POST 请求
【发布时间】:2018-07-12 20:43:45
【问题描述】:
String urly = "myurl";
URL url = new URL(myurl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/xml");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(xml);
wr.flush();

我不确定这是否使用存储在字符串“xml”中的 xml 请求结构发送请求。我不知道使用 XML 发送请求的任何其他方式。

int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader iny = new BufferedReader(new InputStreamReader(con.getInputStream()));
          String output;
          StringBuffer res = new StringBuffer();

          while ((output = iny.readLine()) != null) {
           res.append(output);
          }
          iny.close();
           wr.close(); 
          //printing result from response
          System.out.println(res.toString());

我收到的响应显示请求无效。

【问题讨论】:

  • 您是否遇到任何错误?您是指 servlet 为 api 吗?
  • 不,我没有收到任何错误,只是我没有得到想要的响应。我将 URL 称为 api。
  • 在这里发送xml 变量中的任何内容。如果 xml 变量包含一个实际的 XML 文件,那么您应该很高兴。如何生成 xml 变量?
  • XML 变量包含 XML 字符串,除了一些需要动态填充的字段外,它大多是硬编码的。我认为可能 XML 是通过服务器上的 writeBytes() 方法以字节写入的,我需要以 XML 格式而不是字节发送请求,知道如何实现吗?

标签: java xml curl


【解决方案1】:

生成的 XML 错误,需要检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 2019-10-19
    • 1970-01-01
    • 2016-05-12
    • 2015-01-26
    • 2018-07-05
    相关资源
    最近更新 更多