gpt4 book ai didi

java - org.apache.http.client.HttpResponseException : Bad Request - Sending JSON request via REST

转载 作者:行者123 更新时间:2023-12-01 09:46:07 30 4
gpt4 key购买 nike

当我尝试运行此程序时,收到错误 org.apache.http.client.HttpResponseException:错误请求。

您能帮我理解应该修改代码的位置吗?

我正在使用以下库httpclient-4.4.1.jarhttpcore-4.4.1.jarcommons-logging-1.1.2.jar

org.apache.http.client.HttpResponseException:错误请求

这是代码:

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class Test {
public static void main(String args[]) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://localhost:8080/engine-rest/process-definition/key/demo-scaling/start");
try {
StringEntity input = new StringEntity("(\"variables\":{}, \"businessKey\" : \"AAA001\")");
postRequest.addHeader("Accept", "application/json");
postRequest.setEntity(input);
postRequest.addHeader("Content-Type", "application/json");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(postRequest, responseHandler);
System.out.println(responseBody);
} catch (Exception e) {

e.printStackTrace();
} finally {
httpclient.getConnectionManager().shutdown();
}
}

}

最佳答案

好的,试试这个:

 HttpClient httpClient = HttpClientBuilder.create().build();

try {
HttpPost request = new HttpPost("http://localhost:8080/engine-rest/process-definition/key/demo-scaling/start");
StringEntity params =new StringEntity("variables={\"businessKey\":\"AAA001\"}");
request.addHeader("content-type", "application/x-www-form-urlencoded");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);

System.out.println(response);
}catch (Exception ex) {
// handle Exceptions
}

使用 httpclientbuilder 获取客户端(DefaultHttpClient -> 已弃用),我不确定您是否有有效的 JSON 数据,这只是我的建议。

关于java - org.apache.http.client.HttpResponseException : Bad Request - Sending JSON request via REST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38014910/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com