Org.jsoup.HttpStatusException:HTTP error fetching URL. status=403

Source: Internet
Author: User

conn = jsoup.connect (URL) when crawling a Web site. Timeout. get (); Directly with the Get method, some sites can crawl normally.

However, some websites report 403 errors, and 403 is a common error message during site access. Indicates that the resource is not available, the server understands the client's request, but refuses to handle it, usually caused by a Web access error due to the permissions set on the file or directory on the server.

The solution is just from these angles: Useragent,referer,token,cokkie

So we add the header of the simulation browser to the connection:

[Java]View PlainCopy
  1. <span style="WHITE-SPACE:PRE;"  > </span>conn = Jsoup.connect (URL). Timeout (5000);
  2. Conn.header ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
  3. Conn.header ("accept-encoding", "gzip, deflate, sdch");
  4. Conn.header ("Accept-language", "zh-cn,zh;q=0.8");
  5. Conn.header ("user-agent", "mozilla/5.0" (Windows NT 10.0;  WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/55.0.2883.87 safari/537.36 ");

Then execute Conn.get ()

We can get the data.

Org.jsoup.HttpStatusException:HTTP error fetching URL. status=403

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.