尝试在 HTTPCLIENT -JAVA 中发送 POST 请求时,收到 400 个错误请求

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


我尝试在Eclipse中编写JAVA代码,并收到404错误请求并尝试通过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请求。


MMMHUHU
浏览 285回答 1
1回答

白板的微信

这里的问题是由于几个错误:第一个与输入格式有关。您正在使用的代码尝试映射键和值,但正如我从本指南中看到的那样,它需要纯文本的XML格式作为输入。第二个错误是您尝试通过现有ID发布。在这种情况下,要创建资源,应使用&nbsp;http://www.thomas-bayer.com/sqlrest/CUSTOMER/因此,在这种情况下,为了使它工作,请尝试如下操作:&nbsp; &nbsp; String url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/";&nbsp; &nbsp; &nbsp; &nbsp; HttpClient client = HttpClientBuilder.create().build();&nbsp; &nbsp; &nbsp; &nbsp; HttpPost post = new HttpPost(url);&nbsp; &nbsp; &nbsp; &nbsp; String xml = "<resource>";&nbsp; &nbsp; &nbsp; &nbsp; xml += "<ID>102</ID>";&nbsp; &nbsp; &nbsp; &nbsp; xml += "<FIRSTNAME>Apex</FIRSTNAME>";&nbsp; &nbsp; &nbsp; &nbsp; xml += "<LASTNAME>Consultancy</LASTNAME>";&nbsp; &nbsp; &nbsp; &nbsp; xml += "<STREET>Shell Blvd</STREET>";&nbsp; &nbsp; &nbsp; &nbsp; xml += "<CITY>Fremont</CITY>";&nbsp; &nbsp; &nbsp; &nbsp; xml += "</resource>";&nbsp; &nbsp; &nbsp; &nbsp; post.setEntity(new StringEntity(xml));&nbsp; &nbsp; &nbsp; &nbsp; HttpResponse response = client.execute(post);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(response.getStatusLine().getStatusCode());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Response Code: " + response);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(response.getStatusLine().getReasonPhrase());学习另一种使用命令行实用程序等工具对其进行测试的方法也非常有用。例如,您可以像这样发布产品:curlcurl&nbsp;-X&nbsp;POST&nbsp;&nbsp;http://www.thomas-bayer.com/sqlrest/PRODUCT/&nbsp;-d&nbsp;'<resource><ID>103</ID><NAME>X</NAME><PRICE>2.2</PRICE></resource>'一旦你解决了这个问题,与HTTP代码一起使用将是很重要的。例如,500 错误意味着服务器端出现问题,而 404 通常意味着您命中了无效的终结点(它不存在)。最后,我不会讨论为什么你使用这个项目将HTTP请求发送到服务器 - 但请记住,这不是一种非常常见的方法。目前,带有JSON的REST将更加有趣和令人愉快:)如果您对此感兴趣,请查看弹簧启动REST
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java