I'm developing an Android application, it is like a browser, has a web-view and sends http(s) requests. For debugging purpose, I want to be able to see the requests sent and received by the application (in particular the headers), but I'm unable to do so.
What i tried
I mainly debug the app with Chrome on PC, and use the Network Inspector in Chrome. The problem is that my app uses the ShouldInterceptRequest to intercept the requests an then manually sends a request with cronet. Chrome, in this case, shows some "provisional headers" that are from the original request, an not the headers of the actual request i sent manually.
I tried to use Fiddler and HTTP Toolkit, but the server I'm communicating with, doesn't like the certificate they use, so they can monitor correctly, but, if active, i cannot reach the page i need to monitor.
I also tried the Android Studio network inspector, but seems it work only for HttpURLConnection and Okhttp(1)
Thanks for your time.
If you're using a WebView, set a custom WebViewClient and override shouldInterceptRequest. That will pass in a WebResourceRequest object that will include all headers.
Related
As you know, a lot of request (images, scripts, AJAX, etc.) are send when loading a single page. So I need to get all those request and inspect them.
So the question would be: How can I inspect the HTTP requests that are made when a WebView loads a page ?
I want: headers, method, status code, response, cookies.
Right now, I have:
public void onLoadResource(WebView view, String url){
Log.d("my-tag", "onLoadResource = " + url );
}
But that only shows me the URL.
The best you can get in your app is the WebViewClient.shouldInterceptRequest method, but that only has the URL. You currently can't get any of the things you've listed.
For debugging you can use Chrome DevTools if you're using Android 4.4.
Look here: https://gist.github.com/kibotu/32313b957cd01258cf67 where you get the http headers at android >=21
The easiest way is to use a proxy. I use Charles, but I'm sure there are others. On the device, go to the WiFi settings, long click the one you're connected to, select "modify network" and enable the advanced options. There you'll be abel to configure the proxy settings for the whole device.
is there a way to get request and response objects from a webview?
for requests made from some webpage running in my webview, i want to intercept the full http request object(the headers, http method used, http body etc) and divert and send across that request into another channel.
For responses received from the webview, i want to do the same and get the object and its properties.
So far i have looked at the webviewClient android class which allows you to intercept url links executed by a webpage and intercept the resources it loads.
However, what i want to intercept, is any actual http requests the webpage makes. is this possible in Android webview?
thanks
That is not directly possible. You are welcome to write an HTTP proxy, then attempt to get WebView to work with that (e.g., see if it supports the http.proxyHost and http.proxyPort system properties).
We have discovered a strange bug in on of our Android apps in that the app cannot connect to the internet over cellular on certain Android Models/Mobile Networks (connecting via WiFi works perfectly). The URL which the app is trying is to reach is however accessible using the device's normal browser, which rules out the URL being blocked upstream. Any ideas what could be causing this would be appreciated please.
which rules out the URL being blocked upstream
Not necessarily. As one of the commenters pointed out, you do not indicate how you are using this URL in your app. If this is an HTTP request, and it works in a browser but not your app, try changing the User-Agent HTTP header of your request to match the one from the browser.
You have provided no error log, but from experience I have run into a few inconsistencies when connecting to a server.
Using HttpsUrlConnection a SNI header is set on HoneyComb and above but not on previous versions, which can alter how the server responds. To add to this there are some general SSL handling inconsistencies between API levels, such as handling of wildcard domains in a certificate is buggy in some api levels.
Second some phones/api levels add a header to HttpUrlConnection requests that specifies time the request was sent like so:
X-Android-Sent-Millis=1353085024868
Some servers seem to use these headers to detect mobile traffic and alter the response.
I had problems when using a dual-simcard. Do you have more devices connected? Try to shut them down.
I have packaged a Sencha Touch 2 app with PhoneGap for Android. I can't get the ajax requests to work on my native application. When I handle the request through the browser on http://mydomain.com/mysenchaapp/android/assets/www/ it works fine, however, when I run my app through the emulator as a native app my request fails. I think it would be helpful if I could get fiddler to work on handling native apps' request as well, but I haven't been able to figure that out either. Any help is appreciated.
On a native application you can use cross-domain get requests without using JSONP. I got it to work. My problem accessing the servlet was another issue.
You can't use Ajax for Cross-Domain GET requests, you should use JSONP instead. Take a look at the documentation for Ext.data.JsonP.request(), You can however use Ajax to make a post if i'm not mistaken. This comes in handy when uploading something like a base64 string (which is too big for a GET request)
I have a ASP.NET website deployed to IIS with a couple of ashx that returns JSONs to be consumed by an Android application.
I have implemented an authentication logic using Basic Authentication.
The problem: When accessed from Android, the server response is a 400 Bad request. The httperr log file says "400 - Hostname -".
It works when I try it out on localhost from Android emulator
It works when accessing the ashx file on the server from a browser
It works when replicating the call in Fiddler
(If I use Fiddler with the Android Emulator, the Response will be -1 (and looking in Fiddler at the raw data sent, it seems to loose the host from the url) - but this is another issue so don't dwell on that, i just thought I would mention it...)
Turns out my problem (and solution) was the same as in this thread:
HTTP POST request with authorization on android