43

I am tasked with writing an authentication component for an open source JAVA app. We have an in-house authentication widget that uses https. I have some example php code that accesses the widget which uses cURL to handle the transfer.

My question is whether or not there is a port of cURL to JAVA, or better yet, what base package will get me close enough to handle the task?

Update:

This is in a nutshell, the code I would like to replicate in JAVA:

$cp = curl_init();
$my_url = "https://" . AUTH_SERVER . "/auth/authenticate.asp?pt1=$uname&pt2=$pass&pt4=full";
curl_setopt($cp, CURLOPT_URL, $my_url);
curl_setopt($cp, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($cp);
curl_close($cp);

Heath, I think you're on the right track, I think I'm going to end up using HttpsURLConnection and then picking out what I need from the response.

Community
  • 1
  • 1
JK.
  • 1,121
  • 3
  • 13
  • 18

6 Answers6

65

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();
Heath Borders
  • 30,998
  • 16
  • 147
  • 256
22

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

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

Edit: Here's the SSL Guide

ScArcher2
  • 85,501
  • 44
  • 121
  • 160
  • 3
    Update: That `Commons HttpClient` project is now end-of-life, and has been supplanted by the [`Apache HttpComponents™`](http://hc.apache.org/) project. – Basil Bourque Mar 16 '14 at 23:06
4

jsoup

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

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Try Apache Commons Net for network protocols. Free!

Jason Cohen
  • 81,399
  • 26
  • 107
  • 114
  • No HTTP Client in this library, which is a shame because otherwise Apache Commons is very good (speaking from *experience*): http://commons.apache.org/net/ – therobyouknow Feb 05 '10 at 15:26
1

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

WMR
  • 12,661
  • 5
  • 35
  • 30
0

you can try curl-to-java lib to convert 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;
}
zmag
  • 7,825
  • 12
  • 32
  • 42
jeffrey
  • 9
  • 3