Java ApacheHTTP客户端POST抛出500
我正在尝试使用ApacheHTTP客户端执行POST请求 下面是代码片段Java ApacheHTTP客户端POST抛出500,java,apache,rest,http,apache-httpclient-4.x,Java,Apache,Rest,Http,Apache Httpclient 4.x,我正在尝试使用ApacheHTTP客户端执行POST请求 下面是代码片段 CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://xav.com/asd"); JSONObject jon = new JSONObject(); jon.put("param", "val");
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的错误。我可以使用现有的招摇过市UI执行相同的POST请求。我假设我的请求的构造有问题,并且参数没有正确地传递到后端
我是否正确创建了POST请求?有没有办法在添加参数后获取实际的URL?它只起作用,返回404,而不是500
...
<title>Error 404 Document Not Found on http://xav.com/asd</title>
...
下面是我使用的库:
<!-- 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>
以下是节目:
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();
}
}
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,因为这个网站在我的答案中不存在,我也尝试过:,它得到200。我的建议是,仔细检查您正在使用的库。好的,有没有办法查看由http客户端创建的完整格式的url?不确定您是否需要
httpPost.getURI()
。(参见更新答案中的第二个示例)
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();
}
}