Android Webkit uses URL to determine whether the webpage is a redirected CODE 3XX

Source: Internet
Author: User
Tags response code unsupported

Android Webkit uses URL to determine whether the webpage is a redirected CODE 3XX

To solve the problem that redirection causes a browser and goback is still bothering me

Finally, the solution is to determine the 3xx Redirection url and maintain a returned list history.


The code is relatively simple.

The code 200 is always obtained before. The get request is used and the redirected code is directly returned.

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(cmd.exe cute (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.exe cute (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
 
  
{@ 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 redirection status code is from another blog:

Some common status codes are as follows:

  • 200-The server returns the webpage successfully.
  • 404-The requested webpage does not exist.
  • 503-The server is temporarily unavailable.

The following provides a complete list of HTTP status codes. Click the link to learn more. You can also visit the W3C page on the HTTP status code to learn more.

1xx (temporary response)The status code that indicates a temporary response and requires the requester to perform the operation to continue.

Code Description
100 (continue) The requester shall continue to make the request. The server returns this code, which means that the server has received the first part of the request and is waiting to receive the remaining part.
101 (switching Protocol) The requester has requested the server switching protocol, and the server has confirmed and is ready to switch.

2xx (successful)

The status code indicating that the server has successfully processed the request.

Code Description
200 (successful) The server has successfully processed the request. Generally, this indicates that the server provides the requested webpage. If your robots.txt file is in this status, this indicates that Googlebot has successfully retrieved the file.
201 (created) The request is successful and the server has created a new resource.
202 (accepted) The server has accepted the request but has not processed it.
203 (unauthorized information) The server has successfully processed the request, but returns information that may come from another source.
204 (NO content) The server successfully processes the request but does not return any content.
205 (reset content) The server successfully processes the request but does not return any content. Unlike the 204 response, this response requires the requester to reset the document view (for example, clear the form content to enter new content ).
206 (partial content) The server successfully processes some GET requests.

3xx (redirected)To complete the request, you must perform further operations. These status codes are usually always redirected. Google recommends that you use fewer than five redirection entries for each request. You can use the website administrator tool to check if Googlebot encounters any problems when crawling your redirected webpage. The capture error page under diagnosis lists URLs that cannot be crawled by Googlebot due to redirection errors.

Code Description
300 (multiple options) The server can perform multiple operations based on the request. The server selects an operation based on the User agent or provides an operation list for the User agent to select.
301 (permanent movement) The requested webpage has been permanently moved to a new location. When the server returns this response (as a response to a GET or HEAD request), it automatically redirects the requester to a new location. You should use this code to notify Googlebot that a webpage or website has been permanently moved to a new location.
302 (temporary movement) The server is currently responding to requests from different web pages, but the requester should continue to use the original location for future requests. Similar to code 301 In response to GET and HEAD requests, this code automatically redirects the requester to different locations. However, because Googlebot continues to capture the original location and indexed it, you should not use this code to notify Googlebot that a page or website has been moved.
303 (view other locations) The server returns this code when the requester must perform separate GET requests at different locations to retrieve the response. For all requests except HEAD requests, the server automatically redirects to other locations.
304 (not modified)

The requested webpage has not been modified since the last request. When the server returns this response, no webpage content is returned.

If the web page has not been changed Since the last request, you should Configure the server to return this response (known as the If-Modified-Since HTTP header ). The server can tell Googlebot that the webpage has not been changed since the previous capture, thus saving bandwidth and overhead.

.
305 (use proxy) The requester can only use a proxy to access the requested webpage. If the server returns this response, the server also specifies the proxy that the requester should use.
307 (temporary redirection) The server is currently responding to requests from different web pages, but the requester should continue to use the original location for future requests. Similar to code 301 In response to GET and HEAD requests, this code automatically redirects the requester to different locations. However, because Googlebot continues to capture the original location and indexed it, you should not use this code to notify Googlebot that a page or website has been moved.

4xx (request error)These status codes indicate that the request may fail and the server may be unable to process the request.

Code Description
400 (incorrect request) The server does not understand the request syntax.
401 (unauthorized) The request requires authentication. After logon, the server may return this response to the page.
403 (Forbidden) The server rejects the request. If this status code is displayed when Googlebot tries to capture a valid webpage on your website (you can go to the Google website administrator toolDiagnosisUnderNetwork captureThis status code is displayed on the page). This may be because your server or host rejects Googlebot access to it.
404 (not found)

The server cannot find the requested webpage. For example, if the request is for a webpage that does not exist on the server, the server usually returns this code.

If the robots.txt file does not exist on your website and you find this status on the robots.txt page of the Google website administrator tool diagnostic tag, this is the correct status. However, if you have a robots.txt file and find this status again, it indicates that your robots.txt file may be named incorrectly or located in the wrong location. (The file should be on a top-level domain name and be named robots.txt ).

If you find this status (on the HTTP Error page of the "diagnosis" tab) on the URL that Googlebot is trying to crawl, this indicates that Googlebot may be tracking invalid links in another webpage (old links or incorrect links ).

405 (method disabled) Disable the method specified in the request.
406 (not accepted) You cannot use the Content features of the request to respond to the requested webpage.
407 (proxy authorization required) This status code is similar to 401 (unauthorized), but specifies that the requester should use a proxy for authorization. If the server returns this response, the server also specifies the proxy that the requester should use.
408 (request timeout) The server timed out while waiting for the request.
409 (conflict) A conflict occurs when the server completes the request. The server must contain information about conflicts in the response. The server may return this code when responding to a PUT request that conflicts with the previous request, and provides a list of differences between the two requests.
410 (Deleted) 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 it may replace the 404 code when the resource exists but does not exist now. If the resource has been permanently deleted, you should use code 301 to specify a new location for the resource.
411 (valid length required) The server will not accept requests that contain invalid Content Length header fields.
412 (not meeting the prerequisites) The server does not meet one of the prerequisites set by the requester in the request.
413 (the Request Entity is too large) The server cannot process the request because the request entity is too large and exceeds the server's processing capability.
414 (the requested URI is too long) The request URI (usually the URL) is too long and cannot be processed by the server.
415 (unsupported media type) The request format is not supported by the request page.
416 (the request range does not meet the requirements) If the request is for an invalid webpage range, the server returns this status code.
417 (expectations not met) The server does not meet the "expected" request header field requirements.

5xx (server error)These status codes indicate that an internal error occurs when the server attempts to process the request. These errors may be server errors rather than request errors.

Code Description
500 (internal server error) The request cannot be completed due to a server error.
501 (not implemented) The server does not have the function to complete the request. For example, if the server cannot identify the request method, the server may return this code.
502 (incorrect gateway) The gateway or proxy server receives an invalid response from the upstream server.
503 (Service unavailable) Currently, servers cannot be used (due to overload or downtime maintenance ). This is usually a temporary state.
504 (gateway timeout) As a gateway or proxy, the server does not receive requests from the upstream server in a timely manner.
505 (unsupported HTTP Version) The server does not support the HTTP protocol version used in the request.



Related Article

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.