什么将等同于在 Java 中遵循 curl 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24454164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:17:49  来源:igfitidea点击:

What will be the equivalent to following curl command in java

javacurl

提问by Kashif Ali

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

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

$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");

采纳答案by Jan Wegner

Http(s)UrlConnectionmay be your weapon of choice:

Http(s)UrlConnection可能是您选择的武器:

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();
}

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

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

回答by Oli Bates

Have a look at the java.net.URL and java.net.URLConnection libraries.

查看 java.net.URL 和 java.net.URLConnection 库。

URL url = new URL("yourUrl.com");

Then use a an InputStreamReader & BufferedReader.

然后使用 InputStreamReader 和 BufferedReader。

More information in Oracles example: http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html

Oracles 示例中的更多信息:http: //docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html

This might also help: How to use cURL in Java?

这也可能有帮助:How to use cURL in Java?