2

In desktop browsers, an extension can trivially obtain the status codes of web pages opened in the browser. By status code I mean 200, 302, 404, 502 etc ...

HTTP response status codes

Response.status

Now say that we have an Android App which is similar to the desktop Chrome extension. This app opens URL links not in a WebView, but in an Android browser.

My question is, is there a way for the app to get the status code of web pages opened in an Android browser? Programmatically through Java, within the Android runtime ... ?

There may just be a way to get these through the Browser content provider or through WebResourceResponse, and I am looking into that.

I do not mean getting the status code of a page downloaded in WebView. There is already a question covering that:

Get HTTP Status Code in Android WebView.

I mean getting the status code of a page opened in an Android browser. How can we get these?

Web-based content

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • I know it's not the best way, but the simple idea could be writing a VPN service and catch all requests and responses. – Mir Milad Hosseiny Jan 14 '20 at 20:39
  • @MirMiladHosseiny Assuming we write this `Service`, is there a way to know which request/response belongs to the foreground browser for the webpage it loaded? Because there would in all likelihood be several request/responses occuring in parallel. – Yash Sampat Jan 16 '20 at 06:28
  • As I know, in VPN service you can get specific app requests/responses. In addition, you can get the foreground app and check it is a browser or not. – Mir Milad Hosseiny Jan 17 '20 at 08:57

1 Answers1

1

Sample 1:

URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode(); // show code result :)

This is by no means a robust example; you'll need to handle IOExceptions and whatnot. But it should get you started.

If you need something with more capability, check out HttpClient.

Sample 2:

URL url = new URL("http://www.google.com/humans.txt");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode(); // show result :)
Amirhf
  • 380
  • 4
  • 16
  • Good idea! Thank you for taking the time to respond. In my opinion, however, this isn't quite the same thing as getting the status code from a BROWSER. That would be ideal: the browser providing the status code of a downloaded webpage, or at least providing an indication that the webpage has loaded successfully (or not). – Yash Sampat Jan 16 '20 at 04:18
  • You can get the URL from the webview and put it in the example URL ;) – Amirhf Jan 16 '20 at 12:07
  • I understand what you mean. Firstly, I'm using the browser and not a `WebView`. Secondly, while I can obtain the URL, I don't know at this point how to get the status code. Surely the Android browsers too have some internal mechanism for maintaining the status codes. These should be available to an app that uses browsers, just like in Windows a Chrome or Firefox extension can obtain status codes of a downloaded webpage. – Yash Sampat Jan 16 '20 at 12:48
  • Google Chrome : 1)Right-click on the target page and click on the inspect (ctrl + shift + i) 2)Click the Network button at the top of the page 3)And Finally, you can see the status of the code – Amirhf Jan 16 '20 at 13:02
  • 1
    I cant tell if you're being droll or are serious. I need to get the status code of a webpage opened in an Android browser *programmatically* within the Android runtime. Do you know of a way to do this? – Yash Sampat Jan 16 '20 at 13:59