java - 相当于java中的curl命令

标签 java curl

我需要一些帮助来用java编写与下面的curl命令等效的命令。

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header  'cookie: [APP COOKIES];' -d 'sampleFile.json' 'https://url.com'

最佳答案

HttpClient apache 的你可以做到:

...
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;
}

希望这对您有帮助。

编辑:

我添加了发送文件的方式。我放入了一个单独的代码,而不是混合代码(这是一个近似值,基于 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);

关于java - 相当于java中的curl命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49901102/

相关文章:

java - 在android中动态创建不同数量的textView?

java - 在单元测试中使用应用程序类作为实用程序

java - 删除 ListView 中单击的项目的 ID

php - 下载期间如何解决 cUrl 损坏文件

linux - 从需要认证的页面下载网页源码

python - Stem Controller 的新身份不改变IP

java - 无缓冲的 FileInputStream 不支持 .mark(int)

curl - curl -输出到终端的内容是什么?

http - Curl GET/POST 原始数据包括 header ?

java - 如何停止线程中的 block 方法 DatagramSocket.receive()