亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試在 HTTPCLIENT -JAVA 中發送 POST 請求時,收到 400 個錯誤請求

嘗試在 HTTPCLIENT -JAVA 中發送 POST 請求時,收到 400 個錯誤請求

MMMHUHU 2022-09-28 15:33:44
我正在嘗試使用 JAVA HTTPCLIENT 發布請求,在執行此操作時,我收到 404 錯誤請求。我嘗試在Eclipse中編寫JAVA代碼,并收到404錯誤請求并嘗試通過POSTMAN發送請求并收到HTTP狀態500package 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請求。
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

這里的問題是由于幾個錯誤:

  • 第一個與輸入格式有關。您正在使用的代碼嘗試映射鍵和值,但正如我從本指南中看到的那樣,它需要純文本的XML格式作為輸入。

  • 第二個錯誤是您嘗試通過現有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

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的REST將更加有趣和令人愉快:)如果您對此感興趣,請查看彈簧啟動REST


查看完整回答
反對 回復 2022-09-28
  • 1 回答
  • 0 關注
  • 318 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號