我想在 Java 中进行简单的 POST 调用,
我收到 200 响应代码,但是响应消息错误,我被告知在使用表单数据
时有不同的方式进行 Post 调用。以下是我当前用于进行 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;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是通过 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);
Run Code Online (Sandbox Code Playgroud)
这些是我的主要导入内容,以防有人对导入内容感到困惑。
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;
Run Code Online (Sandbox Code Playgroud)
尝试multipart/form-data明确设置内容类型,
post.setHeader("Content-Type", "multipart/form-data");
Run Code Online (Sandbox Code Playgroud)
在你的代码中,
post.setEntity(new UrlEncodedFormEntity(urlParameters));
post.setHeader("Content-Type", "multipart/form-data");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47926 次 |
| 最近记录: |