简体   繁体   English

Java中的cURL等价物

[英]cURL equivalent in Java

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. 我必须通过java来做到这一点。 Can you help me? 你能帮助我吗?

Thanks Jakub 谢谢Jakub

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. StringEscapeUtils来自commons-lang。 http://commons.apache.org/lang/api/org/apache/commons/lang/StringEscapeUtils.html http://commons.apache.org/lang/api/org/apache/commons/lang/StringEscapeUtils.html

Take a look at URLConnection . 看看URLConnection Sun has some examples . Sun有一些例子 It has various subclasses that support some specific HTTP and HTTPS features. 它有各种子类,支持一些特定的HTTP和HTTPS功能。

in addition to the pure Java answers by Quotidian and Yacoby you can try to execute the curl binary as in php. 除了Quotidian和Yacoby的纯Java答案之外,你可以尝试像在php中一样执行curl二进制文件。 Check out how to use the ProcessBuilder class. 查看如何使用ProcessBuilder类。

You can execute commands in Java by using the Runtime with a call to getRuntime() 您可以通过调用getRuntime()使用Runtime来执行Java命令

link to the javadoc for Runtime. 链接到运行时的javadoc

Here's a decent example of using Runtime. 是使用Runtime 一个很好的例子。

Here's a decent example using Runtime or ProcessBuilder. 这是使用Runtime或ProcessBuilder 一个不错的例子。

I hope some of that is helpful. 我希望其中一些有用。

cURL站点链接到Github上的Java Bindings

You can use HtmlUnit API in Java 您可以在Java中使用HtmlUnit API

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM