Examples of Apache-HTTPClient user verification in JSP development,
Examples of Apache-HTTPClient user verification in JSP development
Preface:
In systems outside the microservice framework, we often encounter the problem of using httpClient to call interfaces, except for setting the whitelist, in many cases, identity authentication is required when an interface is called. I reviewed the official documents and found many solutions, but they are not in line with the actual business scenarios. Here we provide a simple and crude solution.
Solution: Use the request header to save the verification information.
Implementation Code:
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; }}
The above is an example of Apache-HTTPClient user verification. If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you. Thank you for your support for this site!