[ACCEPTED]-cURL equivalent in JAVA-curl

Accepted answer
Score: 64

Exception handling omitted:

HttpURLConnection con = (HttpURLConnection) new URL("https://www.example.com").openConnection();
con.setRequestMethod("POST");
con.getOutputStream().write("LOGIN".getBytes("UTF-8"));
con.getInputStream();

0

Score: 23

I'd use the Commons Http Client. There is a contrib class in 3 the project that allows you to use ssl.

We're 2 using it and it's working well.

Edit: Here's 1 the SSL Guide

Score: 4

jsoup

The jsoup library fetches a URL as the first 1 step in its HTML scraping and parsing duties.

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
Score: 1

Try Apache Commons Net for network protocols. Free!

0

Score: 1

You could also try [http://hc.apache.org/](HTTP Components) from the 2 Apache Project if you need more features 1 than the ones provided through Commons Net.

Score: 0

you can try curl-to-java lib to convert 1 curl php code to java code https://github.com/jeffreyning/curl-to-java demo like this

public Object curl(String url, Object postData, String method) {

CurlLib curl = CurlFactory.getInstance("default");
ch = curl.curl_init();
curl.curl_setopt(ch, CurlOption.CURLOPT_CONNECTTIMEOUT, 1000);
curl.curl_setopt(ch, CurlOption.CURLOPT_TIMEOUT, 5000);
curl.curl_setopt(ch, CurlOption.CURLOPT_SSL_VERIFYPEER, false);
curl.curl_setopt(ch, CurlOption.CURLOPT_SSL_VERIFYHOST, false);
String postDataStr = "key1=v1";

curl.curl_setopt(ch, CurlOption.CURLOPT_CUSTOMREQUEST, "POST");
curl.curl_setopt(ch, CurlOption.CURLOPT_POSTFIELDS, postDataStr);
curl.curl_setopt(ch, CurlOption.CURLOPT_URL, "https://xxxx.com/yyy");
Object html = curl.curl_exec(ch);
Object httpCode = curl.curl_getinfo(ch, CurlInfo.CURLINFO_HTTP_CODE);
if (httpCode != null && 200 == Integer.valueOf(httpCode.toString())) {
    return null;
}
return html;
}

More Related questions