[英]Java Post request with form data
我想用 Java 做一個簡單的 POST 調用,
我收到了 200 響應代碼,但是響應消息錯誤,
有人告訴我,在使用表單數據時,有一種不同的方式可以進行 Post 調用。
以下是我當前用於進行后期調用的 Java 代碼 -
private String makePostCall(){
try {
String url = "http://someIp/trusted";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
// add header
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("username", "app_user"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
return result.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
以下是通過 Postman 應用程序運行的 Post 調用示例 -
我指的是以下網站 -
https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
后調用的預期結果應該是一個令牌,即。 字符串值,當前響應為 -1。
這些答案是正確的,但我很難把它變成一個工作代碼。 因此,讓我提供一個可以重復使用的通用且有效的答案。
private static RequestConfig requestConfig = RequestConfig.custom().build();
public HttpResponse postWithFormData(String url, List<NameValuePair> params) throws IOException {
// building http client
HttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
HttpPost request = new HttpPost(url);
// adding the form data
request.setEntity(new UrlEncodedFormEntity(params));
return httpClient.execute(request);
}
List<NameValuePair> urlParameters = new ArrayList<>();
// add any number of form data
urlParameters.add(new BasicNameValuePair("form_key_1", "form_value_1");
urlParameters.add(new BasicNameValuePair("form_key_2", "form_value_2");
// Getting the HTTP Response and processing it
HttpResponse response = postWithFormData("http_url", urlParameters);
HttpEntity entity = response.getEntity();
// String of the response
String responseString = EntityUtils.toString(entity);
// JSON of the response (use this only if the response is a JSON)
JSONObject responseObject = new JSONObject(responseString);
這些是我的主要導入,以防有人對導入的內容感到困惑。
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
通過顯式設置內容類型multipart/form-data
嘗試一下,
post.setHeader("Content-Type", "multipart/form-data");
在您的代碼中,
post.setEntity(new UrlEncodedFormEntity(urlParameters));
post.setHeader("Content-Type", "multipart/form-data");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.