我试图使用发布一个请求,在这样做的同时,我得到了404坏请求。
我尝试在Eclipse中编写JAVA代码,得到404个坏请求,并尝试通过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确定的要求。
发布于 2019-04-09 01:40:40
这里的问题在于很少有错误:
因此,在这种情况下,为了使它工作,尝试如下所示:
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码就很重要了。例如,500个错误意味着服务器端出现了一些错误,而404通常意味着您到达了一个无效的端点(它不存在)。
最后,我将不讨论为什么要使用这个项目向服务器发送HTTP请求-但请记住,这并不是一种非常常见的方式。目前,使用JSON的其余部分将更加有趣和愉快:)如果您对它感兴趣,请看一下弹簧启动休息
https://stackoverflow.com/questions/55583201
复制相似问题