Android的 - 一个URL请求头打开浏览器打开浏览器、Android、URL

2023-09-04 12:15:03 作者:該死的失戀

我有一个具体的要求,我不得不解雇一个网址从我的活动浏览器。我可以用下面的code做到这一点:

I have a specific requirement where I have to fire a url on the browser from my activity. I am able to do this with the following code :

String finalUrl = "http://localhost:7001/display/result.jsp?param=12345";
Intent browserIntent = new Intent(android.content.Intent.ACTION_VIEW,
                        Uri.parse(finalUrl));

现在,我想通过传递'参数'作为一个请求头中的要求,而不是作为查询字符串本身调用result.jsp中。

Now, I want to invoke result.jsp by passing 'param' as a request header in the request and not as the queryString itself.

可有人请指教?

感谢很多提前

修改 即使在请求主体的'参数'POST请求应该是不错。

EDIT Even a POST request with the 'param' in the request body should be fine.

编辑2 该接受的答案是POST请求,而不是为标题。

EDIT 2 The accepted answer is for the POST request, not for the headers.

推荐答案

Android的浏览器支持观看的javascript,例如下面的code可以启动浏览器应用程序显示一个警告对话框:

The Android Browser support 'viewing' javascript, for example the following code can launch the Browser app to show an alert dialog:

    String finalUrl = "javascript:alert('hello')";
    Intent browserIntent = new Intent(android.content.Intent.ACTION_VIEW,
                            Uri.parse(finalUrl));
    startActivity(browserIntent);

一个常见的​​伎俩被JavaScript做的手术后是您通过JavaScript的一个表格,然后提交。因此,从理论上讲code类似下面应该工作的code(一部分是从this帖子):

A common trick to do post operation by javascript is that you create a form by javascript and then submit it. So in theory code like below should work (part of the code is copied from this post):

    //String finalUrl = "http://localhost:7001/display/result.jsp?param=12345";
    String finalUrl = "javascript:" + 
        "var to = 'http://localhost:7001/display/result.jsp';" +
        "var p = {param:'12345',param2:'blablabla',param3:'whatever'};"+
        "var myForm = document.createElement('form');" +
        "myForm.method='post' ;" +
        "myForm.action = to;" +
        "for (var k in p) {" +
            "var myInput = document.createElement('input') ;" +
            "myInput.setAttribute('type', 'text');" +
            "myInput.setAttribute('name', k) ;" +
            "myInput.setAttribute('value', p[k]);" +
            "myForm.appendChild(myInput) ;" +
        "}" +
        "document.body.appendChild(myForm) ;" +
        "myForm.submit() ;" +
        "document.body.removeChild(myForm) ;";
    Intent browserIntent = new Intent(android.content.Intent.ACTION_VIEW,
                            Uri.parse(finalUrl));
    startActivity(browserIntent);