【问题标题】:How to assign a sub parameter in a POST HttpURLConnection?如何在 POST HttpURLConnection 中分配子参数?
【发布时间】:2013-10-14 15:01:24
【问题描述】:

我正在尝试向 Web 服务 API 发出 HttpURLConnection POST 请求。

请求体需要2+级参数,例如:

<fields>
  <field>title</field>
  <field>description</field>
</fields>

现在我有这个,这给了我一个 500 错误:

 URL url = new URL(strUrl);

 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 conn.setRequestMethod("POST");
 conn.setDoOutput(true);
 conn.setRequestProperty("Content-Type", "application/xml");
 conn.setRequestProperty("field", "title");
 conn.setRequestProperty("field", "author");
 conn.connect();

我已通过 Firefox 的 RESTClient 连接到此,但无法通过代码完成。

如何使用.setRequestProperty 评估每个field 值?我也愿意接受更好的方式来完成这个请求。

【问题讨论】:

  • 您需要创建 XML 文档并将其发布到服务器。

标签: java rest httpurlconnection


【解决方案1】:

你做的不行,HttpURLConnection.setRequestProperty是用来设置HTTP头的。

您需要创建 XML 文档并将其发布到服务器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 2016-02-19
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多