JAVA 中的 cURL 等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/116650/
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-11 08:27:54  来源:igfitidea点击:

cURL equivalent in JAVA

javaphpauthenticationcurl

提问by JK.

I am tasked with writing an authentication component for an open source JAVAapp. We have an in-house authentication widget that uses https. I have some example phpcode that accesses the widgetwhich uses cURLto handle the transfer.

我的任务是为开源JAVA应用程序编写身份验证组件。我们有一个使用https. 我有一些例子php访问的代码widget,它使用cURL来办理过户。

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

我的问题是是否有cURLto的端口JAVA,或者更好的是,什么基础包能让我足够接近来处理任务?

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.

Heath,我认为你走在正确的轨道上,我想我最终会使用 HttpsURLConnection ,然后从响应中挑选出我需要的东西。

采纳答案by Heath Borders

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

回答by Jason Cohen

Try Apache Commons Netfor network protocols. Free!

尝试使用Apache Commons Net来获取网络协议。自由!

回答by WMR

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

如果您需要比通过 Commons Net 提供的功能更多的功能,您也可以尝试使用 Apache 项目中的[ http://hc.apache.org/](HTTPComponents)。

回答by ScArcher2

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

我会使用Commons Http Client。项目中有一个contrib类可以让你使用ssl。

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

我们正在使用它并且运行良好。

Edit: Here's the SSL Guide

编辑:这是SSL 指南

回答by Basil Bourque

jsoup

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

jsoup库获取的URL作为其HTML刮解析职责的第一步。

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

回答by jeffrey

you can try curl-to-java lib to convert curl php code to java code https://github.com/jeffreyning/curl-to-javademo 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;
}