Java中的cURL等价物
我有
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 );
在PHP中。
我必须通过java来做到这一点。 你能帮助我吗?
谢谢Jakub
我使用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;
像这样:
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());
编辑:哎呀。 StringEscapeUtils来自commons-lang。 http://commons.apache.org/lang/api/org/apache/commons/lang/StringEscapeUtils.html
看看URLConnection 。 Sun有一些例子 。 它有各种子类,支持一些特定的HTTP和HTTPSfunction。
除了Quotidian和Yacoby的纯Java答案之外,您可以尝试执行curl二进制文件,就像在php中一样。 查看如何使用ProcessBuilder
类。
您可以通过调用getRuntime()
使用Runtime
来执行Java命令
链接到运行时的javadoc 。
这是使用Runtime 的一个很好的例子。
这是使用Runtime或ProcessBuilder 的一个不错的例子。
我希望其中一些有用。
cURL站点具有Java绑定 。
您可以在Java中使用HtmlUnit API
import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage;
像这样:
WebClient webClient = new WebClient(); HtmlPage homePage = webClient.getPage("http://www.google.com"); String homePageString = homePage.asXml();