Android Webkit resolve redirect URL to determine if the webpage is redirected by code 3XX

Source: Internet
Author: User
Tags response code

To resolve a redirect causing the browser to fail GoBack The problem has been bothering me.

Finally, the solution is to judge the 3xx redirect URL and maintain a return list history for you.


The code is simpler and more straightforward on the code.

The previous code 200 has been used, the original is a GET request, directly return to redirect code.

Package Com.example.chongdingxiang;import Java.io.bufferedinputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.net.httpurlconnection;import Java.net.url;import Org.apache.http.HttpResponse; Import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.impl.client.defaultredirecthandler;import Org.apache.http.protocol.httpcontext;import Org.apache.http.util.entityutils;import Android.app.activity;import Android.os.asynctask;import Android.os.Bundle ; Import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;public class MainActivity Extends Activity {private Button button1;private TextView textview;private EditText edittext1;public static final String Tag= "mainactivity"; @Overrideprotected voID onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main) ; button1= (Button) Findviewbyid (R.id.button1); textview= (TextView) Findviewbyid (r.id.textview1); Edittext1= ( EditText) Findviewbyid (R.ID.EDITTEXT1) Button1.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubnew MyTask (). Execute (Edittext1.gettext (). toString ());}}); public int Getpost (string string) {//TODO auto-generated method Stubint code=0;       HttpGet GetMethod = new HttpGet (string);     Defaulthttpclient loginclient = new Defaulthttpclient ();     Myredirecthandler redirecthandler=new Myredirecthandler ();    Loginclient.setredirecthandler (Redirecthandler); try {HttpResponse response=loginclient.execute (getMethod);    Code= response.getstatusline (). Getstatuscode (); LOG.I (TAG, "Rescode =" + Response.getstatusline (). Getstatuscode ()); Get the response code LOG.I (TAG, "result =" + entityutils.tostring (response.getentity (), "Utf-8 "));//Get server response content} catch (Clientprotocolexception e) {//TODO auto-generated catch block E.printstacktrace  ();  } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();  }return Code; }public class MyTask extends Asynctask<string, integer, integer>{@Overrideprotected integer doinbackground ( String ... params) {//TODO auto-generated method stubstring url=params[0];return getpost (URL);} @Overrideprotected void OnPostExecute (Integer result) {//TODO auto-generated method Stubtextview.settext ("code:" + result);}} public class Myredirecthandler extends defaultredirecthandler{@Overridepublic boolean isredirectrequested ( HttpResponse Response,httpcontext context) {//TODO auto-generated method Stubreturn false;}}}
The following redirect status codes are from other blogs:
<p style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " > Some common status codes are: </p><ul style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " ><li><strong>200</strong> -server successfully returned to Web page </li><li><strong>404</ strong> -The requested Web page does not exist </li><li><strong>503</strong> -the server is temporarily unavailable </li></ Ul><p style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " > The following provides a complete list of HTTP status codes. Click on the link to learn more. You can also access the information about the HTTP status code in the pages for details. </p><p style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " &GT;&LT;STRONG&GT;1XX (ad hoc response) </strong> a status code that represents a temporary response and requires the requestor to perform an action to continue. </p><table border= "1" cellspacing= "1" summary= "1XX status codes indicating a provisional response" Cellpadding= "1" style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " ><tbody><tr><th scope= "col" > Code </th><th scope= "Col" > Description </th></tr><tr><td> 100 (continued) </td><td> the requestor shall continue to make the request. The return of this code by the server means that the server has received the first part of the request and is now waiting to receive the remainder. &LT;/TD&GT;&LT;/TR&GT;&LT;TR&GT;&LT;TD&GT;101 (switching protocol) </td><td> requester has requested the server to switch protocols, the server has confirmed and is ready to switch. </td></tr></tbody></table><p style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " &GT;&LT;STRONG&GT;2XX (Success) </strong></p><p style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " > is used to indicate that the server has successfully processed the request's status code. </p><table border= "1" cellspacing= "1" summary= "2XX status codes indicating successful requests" cellpadding= "1 "Style=" Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " ><tbody><tr><th scope= "col" > Code </th><th scope= "Col" > Description </th></tr>< TR&GT;&LT;TD&GT;200 (Success) </td><td> The server has successfully processed the request. Typically, this indicates that the server provided the requested Web page. If you haveThe robots.txt file is displayed for this state, so this means that Googlebot has successfully retrieved the file. </td></tr><tr><td>201 (created) </td><td> the request was successful and the server has created a new resource. </td></tr><tr><td>202 (accepted) </td><td> the server has accepted the request, but it has not been processed. </td></tr><tr><td>203 (non-authorized information) </td><td> the server successfully processed the request, but returned information that might have come from another source. </td></tr><tr><td>204 (no content) </td><td> the server successfully processed the request, but did not return any content. </td></tr><tr><td>205 (reset content) </td><td> The server successfully processed the request, but did not return any content. Unlike the 204 response, this response requires the requestor to reset the document view (for example, to clear the form contents to enter new content). </td></tr><tr><td>206 (partial content) </td><td> Server successfully processed a partial GET request. </td></tr></tbody></table><p style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " &GT;&LT;STRONG&GT;3XX (redirected) </strong>  to complete the request, you need to proceed further. Typically, these status codes are always redirected. Google recommends that you use less than 5 redirects per request. You can use the Webmaster tools to see if Googlebot is having problems crawling your redirected pages. The crawl error page under diagnostics lists the URLs that Googlebot cannot crawl due to redirection errors. </p><tabLe border= "1" cellspacing= "1" summary= "3xx status codes requiring further action" cellpadding= "1" style= "Color:rgb (51, 51 , 51); font-family:arial; font-size:14px; line-height:26px; " ><tbody><tr><th scope= "col" > Code </th><th scope= "Col" > Description </th></tr>< tr><td>300 (multiple choices) </td><td> the server can perform a variety of actions based on the request. The server can select an action based on the requestor (user agent) or provide a list of actions for the requestor to select. </td></tr><tr><td>301 (permanently moved) </td><td> requested page has been permanently moved to a new location. When the server returns this response (as a response to a GET or HEAD request), the requestor is automatically forwarded to the new location. You should use this code to notify Googlebot that a Web page or Web site has been permanently moved to a new location. </td></tr><tr><td>302 (Temporary Move) </td><td> server is currently responding to requests from a Web page in a different location, However, the requester should continue to use the original location for future requests. This code, like the 301 code that responds to the GET and HEAD requests, automatically transfers the requestor to a different location. However, since Googlebot will continue to crawl the original location and index it, you should not use this code to notify Googlebot that a page or site has been moved. </td></tr><tr><td>303 (View other locations) </td><td> when a requester should make a separate GET request for a different location to retrieve the response, The server returns this code. For all requests except the HEAD request, the server automatically goes to a different location. </td></tr><tr><td>304 (not modified) </td><td><p> The requested Web page has not been modified since the last request. When the server returns this response, the Web page content is not returned. </p><p> If the page has not changed since the requestor last requested it, you should configure the server to return this response (known as the If-modified-since HTTP header). Because the server can tell Googlebot that the webpage has not changed since the last crawl, it saves bandwidth and overhead </p>. </td></tr><tr><td>305 (using proxies) </td><td> requesters can only use proxies to access requested Web pages. If the server returns this response, the server also indicates which proxy the requestor should use. </td></tr><tr><td>307 (Temporary redirect) </td><td> Server is currently responding to requests from a Web page in a different location, However, the requester should continue to use the original location for future requests. This code, like the 301 code that responds to the GET and HEAD requests, automatically transfers the requestor to a different location. However, since Googlebot will continue to crawl the original location and index it, you should not use this code to notify Googlebot that a page or site has been moved. </td></tr></tbody></table><p style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " &GT;&LT;STRONG&GT;4XX (Request error) </strong>  These status codes indicate that the request may have failed and that the server has been prevented from processing the request. </p><table border= "1" cellspacing= "1" summary= "4xx status codes indicating that the server could not process the Request "cellpadding=" 1 "style=" Color:rgb (51, 51, 51); font-family:arial; font-size:14px; Line-height:26px; " ><tbody><tr><th scope= "col" > Code </th><th scope= "Col" > Description </th></tr>< tr><td>400 (Bad Request) </td><td> server does not understand the syntax of the request. &LT;/TD&GT;&LT;/TR&GT;&LT;TR&GT;&LT;TD&GT;401 (not authorized) </td><td> request authentication required. After logging in, the server may return this response to the page. </td></tr><tr><td>403 (disabled) </td><td> server denied request. If this status code is displayed when Googlebot tries to crawl a valid webpage on your site (you can do this in the Google Webmaster Tools <strong> Diagnostics </strong> <strong> Network capture </ See this status code on the Strong> page), then this could be your server or host denying Googlebot access to it. </td></tr><tr><td>404 (not found) </td><td><p> the server could not find the requested webpage. For example, if the request is for a webpage that does not exist on the server, the server typically returns this code. </p><p> If you do not have a robots.txt file on your site and you find this status on the robots.txt page of the Google Webmaster Tools Diagnostic tab, this is the correct state. However, if you have a robots.txt file and you find this status, this means that your robots.txt file may be named incorrectly or in the wrong location. (the file should be located on the top-level domain name and should be named Robots.txt). </p><p> If you find this status on the URL that Googlebot is trying to crawl (on the HTTP error page of the Diagnostics tab), this means that Googlebot may be tracking invalid links (old links or incorrectly entered links) on another page. </p></td></tr><tr><td>405 (method Disabled) </td><td> disables the method specified in the request. </td></tr><tr><td>406 (not accepted) </td><td> cannot use the requested content attribute to respond to the requested Web page. </td></tr><tr><td>407 (requires proxy authorization) </td><td> This status code is similar to 401 (unauthorized), but specifies that the requestor should use the proxy for authorization. If the server returns this response, the server also indicates which proxy the requestor should use. </td></tr><tr><td>408 (Request timed out) </td><td> timeout when the server waits for a request. </td></tr><tr><td>409 (conflict) </td><td> The server has a conflict when the request is completed. The server must contain information about the conflicts that occurred in the response. The server may return this code in response to a PUT request that conflicts with the previous request, and will provide a list of differences for two requests. </td></tr><tr><td>410 (Deleted) </td><td> if the requested resource has been permanently deleted, the server returns this response. This code is similar to the 404 (not Found) code, but sometimes replaces the 404 code in cases where resources have previously existed but are no longer present. If the resource has been permanently deleted, you should use 301 code to specify a new location for the resource. </td></tr><tr><td>411 (requires valid length) </td><td> The server does not accept requests that contain invalid content-Length header fields. </td></tr><tr><td>412 (precondition not met) </td><td> server does not meet one of the prerequisites set by the requestor in the request. </td></tr><tr><td>413 (Request entity too Large) </td><td> serverThe request cannot be processed because the request entity is too large to exceed the processing power of the server. </td></tr><tr><td>414 (The requested URI is too long) </td><td> The requested URI (usually the URL) is too long for the server to process. </td></tr><tr><td>415 (Unsupported media type) </td><td> requested format is not supported by the requested page. </td></tr><tr><td>416 (Request scope not required) </td><td> The server returns this status code if the request is for an invalid range of pages. </td></tr><tr><td>417 (unmet expectations) </td><td> server does not meet the requirements of the "expected" Request header field. </td></tr></tbody></table><p style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " &GT;&LT;STRONG&GT;5XX (server error) </strong> These status codes indicate that the server has an internal error while trying to process the request. These errors may be the error of the server itself, not the request.  </p><table border= "1" cellspacing= "1" summary= "5xx status codes indicating that the server had an internal error" cellpadding= "1" style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; line-height:26px; " ><tbody><tr><th scope= "col" > Code </th><th scope= "Col" > Description </th></tr>&LT;TR&GT;&LT;TD&GT;500 (Server internal error) </td><td> the server encountered an error and could not complete the request. &LT;/TD&GT;&LT;/TR&GT;&LT;TR&GT;&LT;TD&GT;501 (not implemented) </td><td> server does not have the ability to complete the request. For example, the server may return this code when the server does not recognize the request method. </td></tr><tr><td>502 (Bad Gateway) </td><td> server as a gateway or proxy, received an invalid response from the upstream server. </td></tr><tr><td>503 (Service Unavailable) </td><td> currently unable to use the server (due to overloading or downtime maintenance). Usually, this is just a temporary state. </td></tr><tr><td>504 (Gateway Timeout) </td><td> Server as a gateway or proxy, not receiving requests from upstream servers in a timely manner. </td></tr><tr><td>505 (HTTP version not supported) </td><td> server does not support the HTTP protocol version used in the request. </td></tr></tbody></table>



Android Webkit resolve redirect URL to determine if the webpage is redirected by code 3XX

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.