Httpclient sends HTTPS requests

Source: Internet
Author: User

The main idea of sending HTTPS requests is to ignore certificate verification.

/***** @ Param URL * @ Param contexttype "image/JPEG", "application/JSON" * @ return */public static byte [] sendhttpsgeturl (httpclient httpclient1, string URL, string contexttype) {// response content byte [] BS = NULL; // create the default httpclient instance // httpclient = new defaulthttpclient (); httpclient = httpclient1; // create trustmanagerx509trustmanager xtm = new x509trustmanager () {public void checkclienttrusted (Login [] Chain, string authtype) throws certificateexception {} public void checkservertrusted (LOGIN [] Chain, string authtype) throws certificateexception {} public x509certificate [] getacceptedissuers () {return New x509certificate [] {}}}; try {sslcontext CTX = sslcontext. getinstance ("SSL"); // use trustmanager to initialize the context. trustmanager is only used by SSL socket CTX. init (null, new trustmanager [] {xtm }, Null); sslsocketfactory Sf = new sslsocketfactory (CTX, sslsocketfactory. allow_all_hostname_verifier); Scheme Sch = new scheme ("HTTPS", 443, SF); httpclient. getconnectionmanager (). getschemeregistry (). register (Sch); // create httpposthttpget httppost = new httpget (URL); httppost. setheader ("Content-Type", contexttype); // execute the POST request httpresponse response = httpclient.exe cute (httppost); // obtain the response entity httpentity enti Ty = response. getentity (); BS = ioutils. tobytearray (entity. getcontent (); If (null! = Entity) {entityutils. consume (entity); // consume response content} return BS;} catch (exception e) {e. printstacktrace ();} finally {// close the connection and release the resource // httpclient. getconnectionmanager (). shutdown ();} return BS ;}


If consecutive requests do not require cookie, you can modify this method. The method parameter does not contain httpclient. In this case, remember to close the connection in the ifinally block.

The following shows Maven required:

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.3</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency>



Httpclient sends HTTPS requests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.