Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL equivalent in Java

Tags:

java

curl

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.

I have to do it via java. Can you help me?

Thanks Jakub

like image 325
Jakub Cermoch Avatar asked Nov 28 '22 11:11

Jakub Cermoch


1 Answers

I use the HttpClient methods:

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

like image 151
Quotidian Avatar answered Dec 05 '22 09:12

Quotidian