简体   繁体   中英

java- Php curl_setopt options equivalent in apache HttpClient

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

Meaning of the PHP cURL constants

From the documentation :

  1. CURLOPT_FORBID_REUSE :

TRUE to force the connection to explicitly close when it has finished processing, and not be pooled for reuse.

  1. CURLOPT_FRESH_CONNECT :

TRUE to force the use of a new connection instead of a cached one.

  1. CURLOPT_RETURNTRANSFER :

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

  1. CURLOPT_FOLLOWLOCATION :

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).

  1. CURLOPT_TIMEOUT :

The maximum number of seconds to allow cURL functions to execute.

  1. CURLOPT_VERBOSE :

TRUE to output verbose information. Writes output to STDERR, or the file specified using CURLOPT_STDERR.

  1. CURLOPT_SSL_VERIFYHOST :

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).

  1. CURLOPT_SSL_VERIFYPEER :

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.

What Apache HttpClient does

  1. CURLOPT_FORBID_REUSE : every connection is a world apart;
  2. CURLOPT_FRESH_CONNECT : see 1.;
  3. CURLOPT_RETURNTRANSFER : you can handle all of a request;
  4. CURLOPT_FOLLOWLOCATION : it auto follows the redirects;
  5. CURLOPT_TIMEOUT : you can set it (see below);
  6. CURLOPT_VERBOSE : you can handle all the log messages (but you set it to false, so default behavior is ok);
  7. CURLOPT_SSL_VERIFYHOST : you can set it (see below);
  8. CURLOPT_SSL_VERIFYPEER : you set it to false, so don't care.

Some code

// 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.

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