HTTPS protocol to download files based on the network address of the server upload problem

Source: Internet
Author: User

HTTPS protocol the server is experiencing the problem of downloading and uploading files based on network address (pkix:unable to find valid certification path to requested target)

Use HttpClient all sites all trusts do not authenticate:

1  Public StaticCloseablehttpclient gethttpclient ()throwsException {2Sslconnectionsocketfactory SSLSF =NULL;3Poolinghttpclientconnectionmanager cm =NULL;4Sslcontextbuilder Builder =NULL;5Builder =NewSslcontextbuilder ();6         //all trusts, no identification.7Builder.loadtrustmaterial (NULL,NewTruststrategy () {8 @Override9              Public BooleanIsTrusted (x509certificate[] x509certificates, String s)throwscertificateexception {Ten                 return true; One             } A         }); -SSLSF =NewSslconnectionsocketfactory (Builder.build (),Newstring[]{"Sslv2hello", "SSLv3", "TLSv1", "TLSv1.2"},NULL, noophostnameverifier.instance); -Closeablehttpclient httpClient =Httpclients.custom () the . Setsslsocketfactory (SSLSF) - . Setconnectionmanager (cm) -. setconnectionmanagershared (true) - . Build (); +         returnhttpClient; -}
View Code

Upload file:

1  Public Static voidPostparams (String filepath) {2String url = "Http://192.188.188.190:8080/dcs.web/upload";//3Closeablehttpclient httpclient =NULL;4         Try {5HttpClient =gethttpclient ();6}Catch(Exception e) {7 e.printstacktrace ();8         }9Closeablehttpresponse response =NULL;TenString result =NULL; One         Try { AHttpPost HttpPost =NewHttpPost (url+ "Upload"); -Multipartentitybuilder Mentitybuilder =multipartentitybuilder.create (). SetMode (httpmultipartmode.browser_compatible); - Mentitybuilder.setcharset (Charset.forname (httputil.enc_utf_8)); theFilebody file =NewFilebody (NewFile (filepath)); -Mentitybuilder.addpart ("File", file); -Stringbody comment =Newstringbody (type, contenttype.application_json); -Mentitybuilder.addpart ("Converttype", comment); +Httpentity reqentity =mentitybuilder.build (); - httppost.setentity (reqentity); +Response =Httpclient.execute (httppost); A             intStatusCode =response.getstatusline (). Getstatuscode (); at             if(StatusCode = =HTTPSTATUS.SC_OK) { -Httpentity resentity =response.getentity (); -result =entityutils.tostring (resentity); - Entityutils.consume (resentity); -Jsonobject Resultjson =jsonobject.fromobject (result); -                //returns whether the information is successful etc. in}Else { -Exeresultentity.message = "Convert PDF Service no response!" "; to             } +}Catch(Exception e) { -Exeresultentity.message = "PDF conversion exception, error message:" +e.getmessage (); theLogger.error ("Request DCs conversion PDF Service Error! ", e); *}finally { $ httpclientutils.closequietly (httpclient);Panax Notoginseng httpclientutils.closequietly (response); -         } the         returnexeresultentity; +}
View Code

Download file:

1  Public Static byte[] downloadfilefromnet (String URL) {2         Try {3HttpGet HttpGet =Newhttpget (URL);4HttpClient HttpClient =gethttpclient ();5HttpResponse response =Httpclient.execute (httpget);6             intStatusCode =response.getstatusline (). Getstatuscode ();7             if(StatusCode = =HTTPSTATUS.SC_OK) {8httpentity entity =response.getentity ();9InputStream is =entity.getcontent ();Ten                 byte[] Filebyte =Readinputstream (IS); One is.close (); A                 returnFilebyte; -             } -}Catch(Exception e) { the e.printstacktrace (); -         } -         return  NULL; -}
View Code

HTTPS protocol to download files based on the network address of the server upload problem

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.