java - how to send authorization fields (username and password) -
i new java. trying build api in have send authorization details.
i getting 'bad request' error.
i following link reference: http://www.baeldung.com/how-to-use-resttemplate-with-basic-authentication-in-spring
public <t> string response(string url, httpentity<string> httpentity) throws exception { resttemplate resttemplate = new resttemplate(); logger.error("url///////////////////"+url); logger.error("httpentity///////////////////"+httpentity); responseentity<string> responseentity = //resttemplate.exchange(url, httpmethod.post, httpentity, string.class); resttemplate.exchange (url, httpmethod.post, httpentity, string.class); string responsebody = responseentity.getbody(); logger.error("responsebody///////////////////"+responsebody); return responsebody; } private httpheaders createheaders(string username, string password){ return new httpheaders() {{ string auth = username + ":" + password; byte[] encodedauth = base64.encodebase64( auth.getbytes(charset.forname("us-ascii")) ); string authheader = "basic " + new string( encodedauth ); set( "authorization", authheader ); }}; } public void response(request request, string url, string requesttype) throws exception { logger.error("requestbody====>" + request.getto()); stream.of(request.getto()).foreach(system.out::println); httpheaders httpheaders = createheaders("xxxxx", "xxxxxx!"); httpheaders.setcontenttype(mediatype.application_json); logger.error("http headers=========>" + httpheaders); httpentity<string> httpentity = new httpentity<string>(request.tostring(), httpheaders); logger.error("final http entity=============>" + httpentity.getbody()); try { infobipresponse(url, httpentity); } catch (exception e) { logger.error("exception ", e); } } }
Comments
Post a Comment