I got a request that need me to translate or in other words finding the equivalent java code for the below PHP code. I currently using Apache HttpClient to handle the task. I have investigate for 4 hours but no result. There are some article about this on the internet but there is no exact answers. My knowledge about PHP is near 0. Any help would be very much appreciate.
curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
From the documentation :
TRUE to force the connection to explicitly close when it has finished processing, and not be pooled for reuse.
TRUE to force the use of a new connection instead of a cached one.
TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).
The maximum number of seconds to allow cURL functions to execute.
TRUE to output verbose information. Writes output to STDERR, or the file specified using CURLOPT_STDERR.
1 to check the existence of a common name in the SSL peer certificate. 2 to check the existence of a common name and also verify that it matches the hostname provided. In production environments the value of this option should be kept at 2 (default value).
FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.
// set the connection timeout value (CURLOPT_TIMEOUT)
int timeout = 15;
RequestConfig config = RequestConfig.custom()
.setSocketTimeout(timeout * 1000)
.setConnectTimeout(timeout * 1000)
.build();
//Create the request
HttpClient client = HttpClientBuilder
.create()
.setDefaultRequestConfig(config)
//set your verifier (CURLOPT_SSL_VERIFYHOST)
.setHostnameVerifier(new AbstractVerifier() {
@Override
public void verify(final String host, final String[]
cns, final String[] subjectAlts) throws SSLException {
verify(host, cns, subjectAlts, true);
}
}
)
.build();
HttpGet request = new HttpGet(url + theData);
//Get the response
HttpResponse response = client.execute(request);
String responseString = EntityUtils.toString(response.getEntity());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.