Examples of Apache-HTTPClient user verification in JSP development,

Source: Internet
Author: User

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!

Related Article

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.