什么将相当于在java中跟随curl命令 [英] What will be the equivalent to following curl command in java

查看:105
本文介绍了什么将相当于在java中跟随curl命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将以下curl命令转换为java命令。

i need to convert the following curl command into java command.

$curl_handle = curl_init ();

curl_setopt ($curl_handle, CURLOPT_URL,$url);`enter code here`
curl_setopt ($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl_handle, CURLOPT_POST, 1);
curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $postfields);

//echo $postfields;

$curl_result = curl_exec ($curl_handle) or die ("There has been a CURL_EXEC error");


推荐答案

Http UrlConnection 可能是您的首选武器:

Http(s)UrlConnection may be your weapon of choice:

public String sendData() throws IOException {
    // curl_init and url
    URL url = new URL("http://some.host.com/somewhere/to/");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    //  CURLOPT_POST
    con.setRequestMethod("POST");

    // CURLOPT_FOLLOWLOCATION
    con.setInstanceFollowRedirects(true);

    String postData = "my_data_for_posting";
    con.setRequestProperty("Content-length", String.valueOf(postData.length()));

    con.setDoOutput(true);
    con.setDoInput(true);

    DataOutputStream output = new DataOutputStream(con.getOutputStream());
    output.writeBytes(postData);
    output.close();

    // "Post data send ... waiting for reply");
    int code = con.getResponseCode(); // 200 = HTTP_OK
    System.out.println("Response    (Code):" + code);
    System.out.println("Response (Message):" + con.getResponseMessage());

    // read the response
    DataInputStream input = new DataInputStream(con.getInputStream());
    int c;
    StringBuilder resultBuf = new StringBuilder();
    while ( (c = input.read()) != -1) {
        resultBuf.append((char) c);
    }
    input.close();

    return resultBuf.toString();
}



我不太确定 HTTPS_VERIFYPEER-东西,但这可能给你一个起点。

I'm not quite sure about the HTTPS_VERIFYPEER-thing, but this may give you a starting point.

这篇关于什么将相当于在java中跟随curl命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆