【问题标题】:While trying to send POST request in HTTPCLIENT -JAVA, getting 400 Bad Request尝试在 HTTPCLIENT -JAVA 中发送 POST 请求时,收到 400 Bad Request
【发布时间】:2019-04-09 09:40:40
【问题描述】:

我正在尝试使用 JAVA HTTPCLIENT 发布请求,但在执行此操作时,我收到了 404 Bad Request。

我尝试在 Eclipse 中编写 JAVA 代码并收到 404 Bad Request 并尝试通过 POSTMAN 发送请求并收到 HTTP 状态 500

package com.apex.customer.service;

public class CustServicePostTest {

    public static void main(String[] args) throws ClientProtocolException, IOException {

        String url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/102";
        //create the http client
        HttpClient client = HttpClientBuilder.create().build();
        //create the post message
        HttpPost post = new HttpPost(url);

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("ID", "102"));
        urlParameters.add(new BasicNameValuePair("FIRSTNAME", "Apex"));
        urlParameters.add(new BasicNameValuePair("LASTNAME", "Consultancy"));
        urlParameters.add(new BasicNameValuePair("STREET", "Shell Blvd"));
        urlParameters.add(new BasicNameValuePair("CITY", "Fremont"));

        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        HttpResponse response = client.execute(post);

        System.out.println(response.getStatusLine().getStatusCode());
        System.out.println("Parameters : " + urlParameters);
        System.out.println("Response Code: " + response);
        System.out.println(response.getStatusLine().getReasonPhrase());

    }

}

我正在寻找 200 OK 请求。

【问题讨论】:

  • 什么是HttpClientBuilder?如果那是 Apache HttpClient,请标记它。并删除 selenium 标签,因为您的代码似乎根本没有使用 Selenium。简而言之,请阅读标签的描述,因为它还明确表示该 httpclient 标签的“请勿使用”。
  • 您为什么认为sqlrest Web 服务端点支持application/x-www-form-urlencoded 有效负载?我不懂德语,但似乎说它需要 XML 或 JSON 甚至 YAML。

标签: java apache-httpclient-4.x


【解决方案1】:

这里的问题是由于几个错误:

  • 首先与输入格式有关。您正在使用的代码尝试映射键和值,但正如我从 this guide 看到的那样,它需要纯文本中的 XML 格式作为输入。
  • 第二个错误是您试图通过现有 ID 发帖。在这种情况下,要创建资源,您应该使用 http://www.thomas-bayer.com/sqlrest/CUSTOMER/

因此,在这种情况下,为了使其正常工作,请尝试以下操作:

        String url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/";
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);

        String xml = "<resource>";
        xml += "<ID>102</ID>";
        xml += "<FIRSTNAME>Apex</FIRSTNAME>";
        xml += "<LASTNAME>Consultancy</LASTNAME>";
        xml += "<STREET>Shell Blvd</STREET>";
        xml += "<CITY>Fremont</CITY>";
        xml += "</resource>";

        post.setEntity(new StringEntity(xml));
        HttpResponse response = client.execute(post);

        System.out.println(response.getStatusLine().getStatusCode());
        System.out.println("Response Code: " + response);
        System.out.println(response.getStatusLine().getReasonPhrase());

学习另一种使用curl 命令行实用程序等工具对其进行测试的方法也非常有用。例如,您可以发布这样的产品:

curl -X POST  http://www.thomas-bayer.com/sqlrest/PRODUCT/ -d '<resource><ID>103</ID><NAME>X</NAME><PRICE>2.2</PRICE></resource>'

一旦你解决了这个问题,熟悉HTTP codes 就很重要了。例如,500 错误意味着服务器端出现问题,而 404 通常意味着您访问了无效的端点(它不存在)。

最后,我不会讨论您为什么要使用这个项目向服务器发送 HTTP 请求 - 但请记住,这不是一种非常常见的方式。目前,带有 JSON 的 REST 会更加有趣和令人愉快 :) 如果您对此感兴趣,请查看 Spring Boot REST

【讨论】:

  • 感谢您对此的投入。我没有意识到它是 XML 格式的。我是初学者,正在学习 httplclient 概念。我也尝试了上面的代码,我收到了 500 响应代码:内部服务器错误
  • 很高兴它有帮助。 HTTP 请求一开始有点棘手,但很快你就会掌握它:)
  • 刚刚意识到您通过尝试上面的代码提到了 500 错误。这可能是因为 ID 102 已经存在。尝试更改 ID,它会按预期工作:)
猜你喜欢
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
相关资源
最近更新 更多