详解JSP研发中Apache-HTTPClient 用户验证的方法
发布时间:2023-06-13 13:21:16 所属栏目:教程 来源:
导读:在开发程序的过程中,我们经常遇到调用接口的问题,很多时候我们还需要在调用接口时进行身份验证,今天就让错新技术频道小编和大家详解JSP开发中Apache-HTTPClient 用户验证的方法,希望对你有所帮助。
详解JSP开
详解JSP开
在开发程序的过程中,我们经常遇到调用接口的问题,很多时候我们还需要在调用接口时进行身份验证,今天就让错新技术频道小编和大家详解JSP开发中Apache-HTTPClient 用户验证的方法,希望对你有所帮助。 详解JSP开发中Apache-HTTPClient 用户验证的方法 解决方法:利用请求头,将验证信息保存起来。 实现代码: public class HttpClientUtils { protected static final Logger LOG = LoggerFactory.getLogger(HttpClientUtils.class); private static final String AUTHENKEY = "Authorization"; private static final String BASICKEY = "Basic "; public static String getConnect(String url,String username,String password) { CloseableHttpResponse response = null; CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); Base64 token = new Base64(); String authenticationEncoding = token.encodeAsString(new String(username + ":" + password).getBytes()); httpGet.setHeader(AUTHENKEY, BASICKEY + authenticationEncoding); String responseContent = ""; try { response = client.execute(httpGet); HttpEntity entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (IOException e) { LOG.error(e.toString()); } finally { if (response != null) { try { response.close(); } catch (IOException e) { LOG.error(e.toString()); } } if (client != null) { try { client.close(); } catch (IOException e) { LOG.error(e.toString()); } } } return responseContent; }}本文是错新技术频道小编给大家带来的详解JSP开发中Apache-HTTPClient 用户验证的方法,相信大家都有所了解,想要学习到更多的专业知识,欢迎关注错新技术频道小编为大家带来的推荐。 (编辑:聊城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐