Java 中的 cURL 等价物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2156431/
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
cURL equivalent in Java
提问by Jakub Cermoch
I had
我有
exec(
"curl".
" --cert $this->_cCertifikatZPKomunikace".
" --cacert $this->_cCertifikatPortalCA".
" --data \"request=".urlencode($fc_xml)."\"".
" --output $lc_filename_stdout".
" $this->_cPortalURL".
" 2>$lc_filename_stderr",
$la_dummy,$ln_RetCode
);
in php.
在 php 中。
I have to do it via java. Can you help me?
我必须通过java来做。你能帮助我吗?
Thanks Jakub
谢谢雅库布
回答by Quotidian
I use the HttpClient methods:
我使用 HttpClient 方法:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
like so:
像这样:
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod("http://www.google.com");
int responseCode = client.executeMethod(method);
if (responseCode != 200) {
throw new HttpException("HttpMethod Returned Status Code: " + responseCode + " when attempting: " + url);
}
String rtn = StringEscapeUtils.unescapeHtml(method.getResponseBodyAsString());
EDIT: Oops. StringEscapeUtils comes from commons-lang. http://commons.apache.org/lang/api/org/apache/commons/lang/StringEscapeUtils.html
编辑:哎呀。StringEscapeUtils 来自 commons-lang。http://commons.apache.org/lang/api/org/apache/commons/lang/StringEscapeUtils.html
回答by Yacoby
Take a look at URLConnection. Sun has some examples. It has various subclasses that support some specific HTTP and HTTPS features.
看看URLConnection。Sun 有一些例子。它有各种支持某些特定 HTTP 和 HTTPS 功能的子类。
回答by dfa
in addition to the pure Java answers by Quotidian and Yacoby you can try to execute the curl binary as in php. Check out how to use the ProcessBuilderclass.
除了 Quotidian 和 Yacoby 的纯 Java 答案之外,您还可以尝试像在 php 中一样执行 curl 二进制文件。查看如何使用ProcessBuilder该类。
回答by ChadNC
You can execute commands in Java by using the Runtimewith a call to getRuntime()
您可以通过Runtime调用在 Java 中执行命令getRuntime()
link to the javadocfor Runtime.
链接到运行时的javadoc。
Here'sa decent example of using Runtime.
Here'sa decent example using Runtime or ProcessBuilder.
这是一个使用 Runtime 或 ProcessBuilder 的不错示例。
I hope some of that is helpful.
我希望其中一些是有帮助的。
回答by Powerlord
The cURL site links to Java Bindingson Github.
cURL 站点链接到Github上的Java Bindings。
回答by Yatendra Goel
You can use HtmlUnitAPI in Java
您可以在 Java 中使用HtmlUnitAPI
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
like so:
像这样:
WebClient webClient = new WebClient();
HtmlPage homePage = webClient.getPage("http://www.google.com");
String homePageString = homePage.asXml();

