簡體   English   中英

Java中的cURL等價物

[英]cURL equivalent in Java

我有

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和HTTPS功能。

除了Quotidian和Yacoby的純Java答案之外,你可以嘗試像在php中一樣執行curl二進制文件。 查看如何使用ProcessBuilder類。

您可以通過調用getRuntime()使用Runtime來執行Java命令

鏈接到運行時的javadoc

是使用Runtime 一個很好的例子。

這是使用Runtime或ProcessBuilder 一個不錯的例子。

我希望其中一些有用。

cURL站點鏈接到Github上的Java Bindings

您可以在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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM