I would like some help to write in java the equivalent of the curl command below.
我想在java中写一些相当于下面curl命令的帮助。
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'cookie: [APP COOKIES];' -d 'sampleFile.json' 'https://url.com'
3 个解决方案
#1
0
With HttpClient of apache you can do it:
使用apache的HttpClient,你可以做到:
...
import org.apache.http.client.methods.HttpPost;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.Consts;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.HttpEntity;
....
public String sendPost(String url, Map<String, String> postParams,
Map<String, String> header, String cookies) {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
HttpClientBuilder clientBuilder = HttpClients.createDefault();
CloseableHttpClient httpclient = clientBuilder.build();
if (header != null) {
Iterator<Entry<String, String>> itCabecera = header.entrySet().iterator();
while (itCabecera.hasNext()) {
Entry<String, String> entry = itCabecera.next();
httpPost.addHeader(entry.getKey(), entry.getValue());
}
}
httpPost.setHeader("Cookie", cookies);
if (postParams != null) {
Iterator<Entry<String, String>> itParms = postParams.entrySet().iterator();
while (itParms.hasNext()) {
Entry<String, String> entry = itParms.next();
formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
httpPost.setEntity(formEntity);
CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
pageContent = EntityUtils.toString(entity);
}
return pageContent;
}
I hope this help you.
我希望这对你有帮助。
EDIT:
I add the way to send a file. I put in a code separate to not mix code (it is an approximation, based on this examples):
我添加了发送文件的方式。我放入一个单独的代码,不混合代码(这是一个近似,基于这个例子):
File file = new File("path/to/file");
String message = "This is a multipart post";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if (postParams != null) {
Iterator<Entry<String, String>> itParms = postParams.entrySet().iterator();
while (itParms.hasNext()) {
Entry<String, String> entry = itParms.next();
builder.addTextBody(entry.getKey(), entry.getValue(), ContentType.DEFAULT_BINARY);
}
}
builder.addTextBody("text", message, ContentType.DEFAULT_BINARY);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
#2
2
You trying to consume a RESTful web service, so you should use a library like jax-rs or spring REST, this is an example of consuming a RESTful web service, you can find many examples with more details.
您尝试使用RESTful Web服务,因此您应该使用类似jax-rs或spring REST的库,这是使用RESTful Web服务的示例,您可以找到许多具有更多详细信息的示例。
#3
0
I think that the answer you are looking for is which library provides Java to create HTTP requests. What is the best Java library to use for HTTP POST, GET etc.? This question provides you with all the answers that you need
我认为您正在寻找的答案是哪个库提供Java来创建HTTP请求。什么是用于HTTP POST,GET等的最佳Java库?此问题为您提供所需的所有答案
#1
0
With HttpClient of apache you can do it:
使用apache的HttpClient,你可以做到:
...
import org.apache.http.client.methods.HttpPost;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.Consts;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.HttpEntity;
....
public String sendPost(String url, Map<String, String> postParams,
Map<String, String> header, String cookies) {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
HttpClientBuilder clientBuilder = HttpClients.createDefault();
CloseableHttpClient httpclient = clientBuilder.build();
if (header != null) {
Iterator<Entry<String, String>> itCabecera = header.entrySet().iterator();
while (itCabecera.hasNext()) {
Entry<String, String> entry = itCabecera.next();
httpPost.addHeader(entry.getKey(), entry.getValue());
}
}
httpPost.setHeader("Cookie", cookies);
if (postParams != null) {
Iterator<Entry<String, String>> itParms = postParams.entrySet().iterator();
while (itParms.hasNext()) {
Entry<String, String> entry = itParms.next();
formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
httpPost.setEntity(formEntity);
CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
pageContent = EntityUtils.toString(entity);
}
return pageContent;
}
I hope this help you.
我希望这对你有帮助。
EDIT:
I add the way to send a file. I put in a code separate to not mix code (it is an approximation, based on this examples):
我添加了发送文件的方式。我放入一个单独的代码,不混合代码(这是一个近似,基于这个例子):
File file = new File("path/to/file");
String message = "This is a multipart post";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if (postParams != null) {
Iterator<Entry<String, String>> itParms = postParams.entrySet().iterator();
while (itParms.hasNext()) {
Entry<String, String> entry = itParms.next();
builder.addTextBody(entry.getKey(), entry.getValue(), ContentType.DEFAULT_BINARY);
}
}
builder.addTextBody("text", message, ContentType.DEFAULT_BINARY);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
#2
2
You trying to consume a RESTful web service, so you should use a library like jax-rs or spring REST, this is an example of consuming a RESTful web service, you can find many examples with more details.
您尝试使用RESTful Web服务,因此您应该使用类似jax-rs或spring REST的库,这是使用RESTful Web服务的示例,您可以找到许多具有更多详细信息的示例。
#3
0
I think that the answer you are looking for is which library provides Java to create HTTP requests. What is the best Java library to use for HTTP POST, GET etc.? This question provides you with all the answers that you need
我认为您正在寻找的答案是哪个库提供Java来创建HTTP请求。什么是用于HTTP POST,GET等的最佳Java库?此问题为您提供所需的所有答案