spring - Add proxy information and basic auth to the resttemplate using httpClient -
my development environment behind proxy need set proxy information rest template, that's when use httpcomponentsclienthttprequestfactory , set proxy setting in httpclient , set in template.
but have rest service needs basic auth. , set basic auth credentials, need set them in httpclient on rest template. see getparams method in httpclient depricated, can't update existing client in template, , if create new httpclient object, overwrite proxy info set during application bootstrapping.
so there way extract httpclient rest template , update it? or there other way tackle this?
thanks.
configure httpclient
follows:
httphost target = new httphost("hostname", 80, "http"); credentialsprovider credsprovider = new basiccredentialsprovider(); credsprovider.setcredentials( new authscope(target.gethostname(), target.getport()), new usernamepasswordcredentials("user", "passwd")); httphost proxy = new httphost("proxy", 12345); closeablehttpclient httpclient = httpclients.custom() .setproxy(proxy) .setdefaultcredentialsprovider(credsprovider).build(); httpcomponentsclienthttprequestfactory requestfactory = new httpcomponentsclienthttprequestfactory(); requestfactory.sethttpclient(httpclient); resttemplate resttemplate = new resttemplate(requestfactory);
Comments
Post a Comment