這只是工作,並返回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