Launch default browser with intent and post parame

2019-01-14 05:27发布

问题:

Possible Duplicate:
How can I open android browser with specified POST parameters?

I would like to do something like this:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.somepage.com?par1=val1&par2=val2"));

But I dont want to send the parameters with get but with post. How can I do this as described above?

Many thanks in advance, navajo

回答1:

It can be done, but in a tricky way.

You can create a little html file with an auto submit form, read it into a string, replace params and embed it in the intent as a data uri instead of a url. There are a couple little negative things, it only works calling default browser directly, and trick will be stored in browser history, it will appear if you navigate back.

Here is an example:

HTML file (/res/raw):

<html>
    <body onLoad="document.getElementById('form').submit()">
        <form id="form" target="_self" method="POST" action="${url}">
            <input type="hidden" name="param1" value="${value}" />
            ...
        </form>
    </body>
</html>

Source code:

private void browserPOST() {
    Intent i = new Intent();
    // MUST instantiate android browser, otherwise it won't work (it won't find an activity to satisfy intent)
    i.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
    i.setAction(Intent.ACTION_VIEW);
    String html = readTrimRawTextFile(this, R.raw.htmlfile);

    // Replace params (if any replacement needed)

    // May work without url encoding, but I think is advisable
    // URLEncoder.encode replace space with "+", must replace again with %20
    String dataUri = "data:text/html," + URLEncoder.encode(html).replaceAll("\\+","%20");
    i.setData(Uri.parse(dataUri));
    startActivity(i);
}

private static String readTrimRawTextFile(Context ctx, int resId) {
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader buffreader = new BufferedReader(inputreader);
    String line;
    StringBuilder text = new StringBuilder();
    try {
        while ((line = buffreader.readLine()) != null) {
            text.append(line.trim());
        }
    }
    catch (IOException e) {
        return null;
    }
    return text.toString();
}


回答2:

Navajo,

What you are trying to do cannot be done with the above URL with the constraints that you have made above. The primary reason for this is that the URL above IS a GET URL. A POST URL does not have the above parameters in it. They are passed in the actual REQUEST and not the URL.

To accomplish what you wish to do, you would have to intercept the Intent, reformat the URL and then start the browser with a new Intent. The source of the URL is the key. If the source is from you or something you can track, that is easy, just create a custom Intent. If the source is outside of your control, then you can run into problems (see below)...

1) GETs and POSTs are not interchangable. If you are messing with data that is not yours or is not going to a site that you control, then you may break the functionality of that site, because not everyone programs for both GETs and POSTs for security reasons.

2) If you are responding to the same Intent that the browser does, then it is possible that the User may not understand what your app does if it always opens the default.

Another possibility, (if you are in control of the website), is to respond to the Intent by creating a cookie that your site can read with the actual data requirements. This would require PHP/ASP on the server or JS activated HttpRequest().

If I had more information I could advise you better.

FuzzicalLogic