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

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

使用基本身份驗證和請求正文在 Java 中發送 HTTP POST 請求

使用基本身份驗證和請求正文在 Java 中發送 HTTP POST 請求

蠱毒傳說 2021-11-11 13:35:07
我正在嘗試通過 Java 向 ServiceNow REST API 發送 HTTP 請求并創建一個事件。我在 Postman 中使用: URL: POST https://<domain>.service-now.com/api/now/table/em_event身體:{    "source":"Postman",    "node":"Codys Mac",    "type":"Test",    "resource":"Test",    "metric_name":"Testing",    "event_class":"Test",    "severity":"4",    "description":"This is a test event, created from REST API.",}它有效,沒有問題。但是在 Java 中,我只有問題。我發現我試圖遵循的最簡單的例子是:https : //kodejava.org/how-do-i-send-an-http-post-request/我的代碼:    public void post(){        HttpClient client = HttpClientBuilder.create().build();        HttpPost post = new HttpPost(serviceNowURL);        List<NameValuePair> arguments = new ArrayList<>();        arguments.add(new BasicNameValuePair("source", "Sample Producer"));        arguments.add(new BasicNameValuePair("node", "Codys Mac"));        arguments.add(new BasicNameValuePair("type", "Test Event Type"));        arguments.add(new BasicNameValuePair("resource", "Test Event Resource"));        arguments.add(new BasicNameValuePair("metric_name", "Test Event Metric Name"));        arguments.add(new BasicNameValuePair("event_class", "Test Event Class"));        arguments.add(new BasicNameValuePair("sevrity", "5"));        arguments.add(new BasicNameValuePair("description", "This is a test event, created by SampleProducer App usign REST API."));        }    }但每當我運行此,我遇到的第一線異常HttpClientBuilder.create().build()的java.lang.NoSuchFieldError: INSTANCE,我不知道為什么。有誰知道我做錯了什么,或者我可以用另一種方法來解決這個問題?
查看完整描述

2 回答

?
慕田峪9158850

TA貢獻1794條經驗 獲得超8個贊

根據這個 QA: HttpClientBuilder - java.lang.NoSuchFieldError: INSTANCE你得到了Apache HTTP Client 的不兼容版本httpcore和httpclient依賴項的混合。


建議:調查瞬態依賴項并嘗試通過從依賴項列表中刪除導致該問題的庫來解決沖突,或者在您的項目中明確定義這兩個庫pom.xml,例如:


<dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpcore</artifactId>

    <version>4.4.10</version>

</dependency>

<dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpclient</artifactId>

    <version>4.5.6</version>

</dependency>

注意:我為httpclient庫選擇了最新的兼容版本。這可能會在未來發生變化,或者可能與您當前使用的版本不匹配。httpclient


查看完整回答
反對 回復 2021-11-11
?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

      CloseableHttpClient httpclient = HttpClients.createDefault();

這對我有用,您不必使用連接管理器打開和關閉連接


查看完整回答
反對 回復 2021-11-11
  • 2 回答
  • 0 關注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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