Android - Open a browser with a URL with request headers

I have a specific requirement when I have to run the browser url from my activity. I can 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)); 

Now I want to call result.jsp, passing "param" as the request header in the request, and not as queryString itself.

Can anyone please the advice?

Thank you very much in advance

EDIT Even a POST request with a β€œparameter” in the request body should be good.

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

+6
source share
1 answer

Android Browser supports javascript viewing, for example, the following code can launch the Browser application to display a warning dialog:

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

The common trick jQuery does after the operation is that you create a javascript form and then submit it. Therefore, in theory, code similar to the one below should work (part of the code was copied from this message ):

  //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); 
+14
source

Source: https://habr.com/ru/post/927713/


All Articles