Resolve HttpClient Access return 403 forbiddent

Source: Internet
Author: User
Tags http request

Create a HttpClient object directly

Closeablehttpclient httpClient = new Httpclients.createdefault ();

This code produces a 403 forbidden because there is a problem with the User-agent field and we need to set the UA field. UA is the identity of the browser identity.
User-agent is part of the HTTP protocol and is part of the header domain, and the User agent is also referred to as UA. In a more general sense, it is an identification of the type of browser you are using, the operating system and version, the type of CPU, the browser rendering engine, the browser language, the browser plugin, and so on, to access the site. The UA string is sent to the server each time the browser HTTP request occurs.
the standard format of the browser UA string is: Browser identification (operating system identity; encryption level identification; browser language) rendering engine identity version information

Solutions

We can use a builder to set up the UA field and then create the HttpClient object
httpclientbuilder builder = Httpclients.custom ();
Understand the meaning of each section against the standard format of the UA string
builder.setuseragent ("mozilla/5.0 (Windows; U Windows NT 5.1;en-us;rv:0.9.4) ");       
Final Closeablehttpclient httpClient = Builder.build ();

Then use the Created object httpclient

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.