【发布时间】: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标签的“请勿使用”。 -
您为什么认为
sqlrestWeb 服务端点支持application/x-www-form-urlencoded有效负载?我不懂德语,但似乎说它需要 XML 或 JSON 甚至 YAML。
标签: java apache-httpclient-4.x