Java Post request with form data(带有表单数据的 Java Post 请求)
问题描述
我想用 Java 进行一个简单的 POST 调用,
我收到 200 响应代码,但是响应消息错误,
有人告诉我,在使用表单数据时,可以通过不同的方式进行 Post 调用.
以下是我当前用于进行 post 调用的 Java 代码 -
I want to make a simple POST call in Java,
I am getting a 200 response code but, with the wrong response message,
I am told there is a different way to make a Post call when using a form data.
Following is my current Java code to make the post call -
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("
Sending '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 call 示例 -
我指的是以下网站 -
https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
post call 的预期结果应该是一个 Token 即.一个字符串值,当前响应为-1.
The expected outcome of the post call is supposed to be a Token ie. a String value, current response is -1.
推荐答案
这些答案是正确的,但我很难将其转化为工作代码.所以,让我提供一个可以重复使用的通用且有效的答案.
These answers are correct, but I struggled to get this to a working code. So, let me provide a general and working answer which can be reused.
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);
这些是我的主要导入,以防有人对导入什么感到困惑.
These are my main imports, in case anyone gets confused on what to import.
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;
这篇关于带有表单数据的 Java Post 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有表单数据的 Java Post 请求
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01