HttpClient return 403 Forbiddenn problem

Source: Internet
Author: User

Want to do a group Bo (itself is some, because the CSDN RSS subscription does not conform to the specification can not be crawled, I intend to manually implement the crawl operation), but through the httpclient Web source access to the time unexpectedly found that the return is 403 forbidden, a little awkward. Then find the information on the Internet after finding that it is to set the request parameters, and then think about whether HttpClient is not what Setparameter method, looked for a bit as sure as it is, and then set the following parameters:

    HttpClient HttpClient = new Defaulthttpclient ();
     Httpclient.getparams (). Setparameter ("User-agent", "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.11 (khtml, like Gecko) chrome/23.0.1271.64 safari/537.11 ");

However useless, since as a request has the request parameter, then should also have the request head, yes, finally found the solution 403 forbidden method, usually, through the browser to request a page, the browser will user-agent included in the request header, If you go directly through httpclient to access a webpage without setting the request header, it is possible for the server to reject the response, so as long as you set up the request header as follows, you can normally access:

    HttpGet httpgets = new HttpGet (URL);
    Httpgets.setheader ("User-agent", "mozilla/5.0" (Windows; U Windows NT 5.1; ZH-CN; rv:1.9.0.3) gecko/2008092417 firefox/3.0.3 ");

This allows you to simulate a browser to request a Web page.

+++++++++++++++++++++++++++++++++++++
Off Topic
Hope that through the blog to help yourself to record some things, to record some problems, perhaps the problem is very small, but when needed to see there will always be a great help.

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.