Apache的HTTP客户端POST抛出500
问题描述:
我试图通过使用Apache HTTP客户端Apache的HTTP客户端POST抛出500
这里执行POST请求的代码片段
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://xav.com/asd");
JSONObject jon = new JSONObject();
jon.put("param", "val");
jon.put("param2", "val2");
String json = jon.toString();
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
CloseableHttpResponse response = client.execute(httpPost);
BufferedReader rd = new BufferedReader
(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
client.close();
,但我得到一个500错误。我可以使用存在的swagger UI执行相同的POST请求。我认为在构建我的请求时出现错误,并且参数没有正确传递到后端。
我是否正确创建了POST请求?在添加参数之后,有没有办法获得实际的URL?
答
这只是工作,并返回404,不500
...
<title>Error 404 Document Not Found on http://xav.com/asd</title>
...
404实际上是在你的榜样http://xav.com/asd正确的反应。
下面是程序:
package com.stackoverflow;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;
public class ApacheHttpClientTest {
public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://xav.com/asd");
JSONObject jon = new JSONObject();
jon.put("param", "val");
jon.put("param2", "val2");
String json = jon.toString();
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
CloseableHttpResponse response = client.execute(httpPost);
BufferedReader rd = new BufferedReader
(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
client.close();
}
}
而且下面是我使用的库:
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
如果你尝试另一个公共Web服务,http://www.posttestserver.com/,你会得到HTTP状态200:
Successfully dumped 0 post variables.
View it at http://www.posttestserver.com/data/2017/05/25/22.39.57585465710
No Post body.
以下是程序:
package com.stackoverflow;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;
public class ApacheHttpClientTest {
public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://posttestserver.com/post.php");
JSONObject jon = new JSONObject();
jon.put("param", "val");
jon.put("param2", "val2");
// Returns the original request URI. Please note URI remains
// unchanged in the course of request execution and is not
// updated if the request is redirected to another location.
System.out.println("URI: " + httpPost.getURI());
// URI: http://posttestserver.com/post.php
String json = jon.toString();
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
CloseableHttpResponse response = client.execute(httpPost);
BufferedReader rd = new BufferedReader
(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
client.close();
}
}
谢谢,请注意,我在本文中使用垃圾URL。所以你会得到404,因为该网站不存在 – AbtPst
在我的答案中,我也试过:http://posttestserver.com/post.php,它得到200.我的建议是,仔细检查你正在使用的库。 – Yuci
好的,有没有办法看到由http客户端创建的完整格式的url? – AbtPst