首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在HTTPCLIENT -JAVA中尝试发送POST请求时,收到了400个坏请求

在HTTPCLIENT -JAVA中尝试发送POST请求时,收到了400个坏请求
EN

Stack Overflow用户
提问于 2019-04-08 23:36:43
回答 1查看 1.5K关注 0票数 1

我试图使用发布一个请求,在这样做的同时,我得到了404坏请求。

我尝试在Eclipse中编写JAVA代码,得到404个坏请求,并尝试通过POSTMAN发送请求,并收到HTTP Status 500

代码语言:javascript
复制
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确定的要求。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-09 01:40:40

这里的问题在于很少有错误:

  • 首先是与输入格式有关。您使用的代码试图映射键和值,但正如我从本指南中看到的那样,它需要纯文本中的XML格式作为输入。
  • 第二个错误是,您试图在现有ID上发布。

因此,在这种情况下,为了使它工作,尝试如下所示:

代码语言:javascript
复制
        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命令行实用工具等工具测试它的另一种方法也非常有用。例如,您可以发布这样的产品:

代码语言:javascript
复制
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的其余部分将更加有趣和愉快:)如果您对它感兴趣,请看一下弹簧启动休息

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55583201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档