Java uses HttpURLConnection to retrieve the 403 error handling method when a website is retrieved, httpurlconnection

Source: Internet
Author: User

Java uses HttpURLConnection to retrieve the 403 error handling method when a website is retrieved, httpurlconnection

How to handle 403 errors when using HttpURLConnection to retrieve a website:

When we access the website through code, an error is reported:



This situation is classified into two types,

1. You need to log on to the console to access the service;

2. You need to set the User-Agent to cheat the server.

connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

User Agent description:

The Chinese name of the User Agent is the User Agent (UA), which is a special string header, the server can identify the operating system and version, CPU type, browser and version, browser rendering engine, browser language, and browser plug-in used by the customer.

Some websites often send different pages to different operating systems and browsers by judging the UA. Therefore, some pages may not be displayed normally in a browser, however, you can bypass detection by disguising UA.

Java Website access code:

/*** Initiate an http get request to obtain the webpage source code * @ param requestUrl * @ param isUserAgent whether to set the spoofing server * @ return */public static String httpRequest (String requestUrl, boolean isUserAgent) {StringBuffer buffer = null; try {// establish a connection URL url = new URL (requestUrl); HttpURLConnection httpUrlConn = (HttpURLConnection) url. openConnection (); httpUrlConn. setDoInput (true); httpUrlConn. setRequestMethod ("GET"); if (isUserAgent) {httpUr LConn. setRequestProperty ("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)") ;}// gets the input stream InputStream inputStream = httpUrlConn. getInputStream (); InputStreamReader inputStreamReader = new InputStreamReader (inputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader (inputStreamReader); // read the returned result buffer = new StringBuffer (); string str = null; while (str = bufferedR Eader. readLine ())! = Null) {buffer. append (str);} // releases the resource bufferedReader. close (); inputStreamReader. close (); inputStream. close (); httpUrlConn. disconnect ();} catch (Exception e) {e. printStackTrace ();} return buffer. toString ();}

Execution result: the webpage code is obtained successfully.





Java uses the HttpURLConnection server. If no problem occurs during sending but the network is disconnected when the result is returned, how can this problem be solved?

. SetConnectTimeout () refers to the timeout time for establishing a connection with the server requesting the URL.
SetReadTimeout () indicates timeout when no data is returned by the server within the specified time after a connection is established.
503 is an error code. If the error code is returned, the server returns response. Timeout indicates that you have not received the server's response at the specified time.

No status code is returned for any timeout. Because the response is in the response, and does not receive the response within the specified time, it will time out. If a timeout occurs, an exception is thrown. You can catch a timeout exception and handle it as needed.

Java issues with HttpURLConnection

Add the following code and try again:
HttpURLConnection. setDoOutput (true );
HttpURLConnection. setDoInput (true );
HttpURLConnection. setRequestMethod ("POST ");

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.