How to intercept http POST data from web form
Asked Answered
G

3

5

I've a web page (being displayed in a browser, NOT a WebView), and I'd like to pass some data (using http POST for instance) to a regular android app.

I only need the app to be launched and fed with data. I know the app can be launched by registering an intent filter, but I'm lost on the data passing part.

There's no need for the URL to be real, a fake URL is fine.

This can be done in BlackBerry using HTTP filters. Is there a way to do the same thing in Android?

Thanks in advance.

Gatling answered 15/5, 2012 at 9:43 Comment(4)
There are some SO Questions #2959201 , #3470408 and https://mcmap.net/q/1699576/-launch-app-with-urlSpathic
@Spathic Thanks, I've read them all. But, as I tried to explain in my question, I already know how to launch the app but not how to pass data.Gatling
check my blogspot html and activity links It contains info about passing data (using GET): sherifandroid.blogspot.com/2011/09/…Frunze
@Sherif +1 for showing how to use getQueryParameter.Gatling
S
4

In your web page put links like:

<a href="my.special.scheme://xyz.com/data1/data2">

In your activity's onCreate() you can access the data through the intent object..

Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "xyz.com"
List<String> params = data.getPathSegments(); 
String first = params.get(0); // "data1"
String second = params.get(1); // "data2"
Stringboard answered 18/5, 2012 at 13:54 Comment(0)
H
2

The approach I would investigate into is to implement a very simple HTTP server service as part of your application. Coding a small web server for your exact purpose would be extremely simple: Listen on TCP port 80, accept incoming connection, then keep reading from socket until \r\n\r\n is seen to skip the header, and then read the POST data. Your web page would presumably access it via 'localhost'. Whether there are any major hurdles with this method I don't know, but on doing a quick search I've seen there are already small Android web servers available, so technically it seems possible.

Hole answered 18/5, 2012 at 7:47 Comment(1)
Humm, but the service is a good candidate to be stopped by the OS.Gatling
G
1

Here are my two cents:

Although not actually intercepting, but this approach does the job using the conventional Intent Filter mechanism.

Example:

  1. Browser opens a link to myownscheme://myfakeurl.fake/http%3A%2F%2Factualurl.com
  2. Android app matches the intent filter and is launched. It parses the parameter in the URL and decodes it obtaining "http://actualurl.com"
  3. The android app makes a connection to the passed url and fetches the data.

        String encodedURL = getIntent().getData().getPathSegments().get(0); //Returns http%3A%2F%2Factualurl.com
        String targetURL = Html.fromHtml(encodedURL); //Returns http://actualurl.com
        //Now do the GET and fetch the data.
    
Gatling answered 18/5, 2012 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.