尝试在 HTTPCLIENT -JAVA 中发送 POST 请求时,收到 400 Bad Request
While trying to send POST request in HTTPCLIENT -JAVA, getting 400 Bad Request
我正在尝试 POST 使用 JAVA HTTPCLIENT 的请求,在这样做时,我收到 404 错误请求。
我尝试在 Eclipse 中编写 JAVA 代码并收到 404 Bad Request 并尝试通过 POSTMAN 发送请求并收到 HTTP Status 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 请求。
这里的问题是由于一些错误:
- 首先与输入格式有关。您正在使用的代码尝试映射键和值,但正如我从 this guide 中看到的那样,它需要纯文本的 XML 格式作为输入。
- 第二个错误是您试图 post 覆盖现有 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
命令行实用程序等工具对其进行测试的方法也非常有用。例如,您可以 POST 这样的产品:
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
我正在尝试 POST 使用 JAVA HTTPCLIENT 的请求,在这样做时,我收到 404 错误请求。
我尝试在 Eclipse 中编写 JAVA 代码并收到 404 Bad Request 并尝试通过 POSTMAN 发送请求并收到 HTTP Status 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 请求。
这里的问题是由于一些错误:
- 首先与输入格式有关。您正在使用的代码尝试映射键和值,但正如我从 this guide 中看到的那样,它需要纯文本的 XML 格式作为输入。
- 第二个错误是您试图 post 覆盖现有 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
命令行实用程序等工具对其进行测试的方法也非常有用。例如,您可以 POST 这样的产品:
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