gpt4 book ai didi

java - JAVA 中带有 JSON 字符串的 HTTP POST 请求

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:19:44 25 4
gpt4 key购买 nike

我必须使用已经生成的 JSON 字符串发出 http Post 请求。我尝试了两种不同的方法:

1.HttpURLConnection
2.HttpClient

但是我从他们两个那里得到了相同的“不需要的”结果。到目前为止,我使用 HttpURLConnection 的代码是:

public static void SaveWorkflow() throws IOException {
URL url = null;
url = new URL(myURLgoeshere);
HttpURLConnection urlConn = null;
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("Content-Type", "application/json");
urlConn.connect();

DataOutputStream output = null;
DataInputStream input = null;
output = new DataOutputStream(urlConn.getOutputStream());

/*Construct the POST data.*/
String content = generatedJSONString;

/* Send the request data.*/
output.writeBytes(content);
output.flush();
output.close();

/* Get response data.*/
String response = null;
input = new DataInputStream (urlConn.getInputStream());
while (null != ((response = input.readLine()))) {
System.out.println(response);
input.close ();
}
}

到目前为止,我使用 HttpClient 的代码是:

public static void SaveWorkflow() {
try {

HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(myUrlgoeshere);
StringEntity input = new StringEntity(generatedJSONString);
input.setContentType("application/json;charset=UTF-8");
postRequest.setEntity(input);
input.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
postRequest.setHeader("Accept", "application/json");
postRequest.setEntity(input);

HttpResponse response = httpClient.execute(postRequest);

BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));

String output;

while ((output = br.readLine()) != null) {
System.out.println(output);
}

httpClient.getConnectionManager().shutdown();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}
}

生成的JsonString是这样的:

{"description":"prova_Process","modelgroup":"","modified":"false"}

我得到的响应是:

{"response":false,"message":"Error in saving the model. A JSONObject text must begin with '{' at 1 [character 2 line 1]","ids":[]}

有什么想法吗?

最佳答案

最后我设法找到了解决问题的办法......

public static void SaveWorkFlow() throws IOException
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(myURLgoesHERE);
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("task", "savemodel"));
params.add(new BasicNameValuePair("code", generatedJSONString));
CloseableHttpResponse response = null;
Scanner in = null;
try
{
post.setEntity(new UrlEncodedFormEntity(params));
response = httpClient.execute(post);
// System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
in = new Scanner(entity.getContent());
while (in.hasNext())
{
System.out.println(in.next());

}
EntityUtils.consume(entity);
} finally
{
in.close();
response.close();
}
}

关于java - JAVA 中带有 JSON 字符串的 HTTP POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20740349/

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