2017-05-25 144 views
0

我試圖通過使用Apache HTTP客戶端Apache的HTTP客戶端POST拋出500

http://hc.apache.org/

這裏執行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?

回答

0

這只是工作,並返回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(); 
    } 
} 
+0

謝謝,請注意,我在本文中使用垃圾URL。所以你會得到404,因爲該網站不存在 – AbtPst

+0

在我的答案中,我也試過:http://posttestserver.com/post.php,它得到200.我的建議是,仔細檢查你正在使用的庫。 – Yuci

+0

好的,有沒有辦法看到由http客戶端創建的完整格式的url? – AbtPst