[英]cURL equivalent in JAVA
I am tasked with writing an authentication component for an open source JAVA
app. 我的任务是为开源JAVA
应用程序编写身份验证组件。 We have an in-house authentication widget that uses https
. 我们有一个使用https
的内部身份验证窗口小部件。 I have some example php
code that accesses the widget
which uses cURL
to handle the transfer. 我有一些示例php
代码,可访问使用cURL
来处理传输的widget
。
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? 我的问题是JAVA
是否有cURL
的端口,或者更好的是,什么基本软件包将使我足够接近以处理任务?
Update : 更新 :
This is in a nutshell, the code I would like to replicate in JAVA: 简而言之,我想在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. 荒地 ,我认为您的目标是正确的,我认为我最终将使用HttpsURLConnection,然后从响应中选择所需的内容。
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();
I'd use the Commons Http Client . 我会使用Commons Http Client 。 There is a contrib class in the project that allows you to use ssl. 项目中有一个contrib类,可让您使用ssl。
We're using it and it's working well. 我们正在使用它,并且运行良好。
Try Apache Commons Net for network protocols. 尝试使用Apache Commons Net获取网络协议。 Free! 自由!
如果您需要比Commons Net提供的功能更多的功能,也可以尝试从Apache项目中尝试[ http://hc.apache.org/](HTTP组件)。
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 您可以尝试curl-to-java lib将curl php代码转换为Java代码https://github.com/jeffreyning/curl-to-java演示,例如
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.