Search code examples
javaandroidwebviewsubmitcsrf

Android Java : Error when submit form(post) on Webview


I want to load in webview this page, https://pkfieur.gares.io/pk2geo/, and past data in "Ligne" and "PK" and click on "Rechercher".

Past data isn't a problem, but the submit yes.

Here the html:

<form id="form-id" method="post">
  <div id="the_post">...</div>
  <input type="submit" value="Rechercher">
</form>

And the code :

web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);
web.loadUrl("https://pkfieur.gares.io/pk2geo/");

go.setOnClickListener(new View.OnClickListener()     {
@Override
public void onClick(View v) {
   web.loadUrl("javascript: document.getElementById('id_ligne').value ='" +  ligne.getText() + "';" 
+ "document.getElementById('id_pk').value ='" + pk.getText() + "';" 
+ "document.getElementById('form-id').submit();");
 }

});

When my edit text is ready, i click on my button GO (in my app) and the webview telling me this :

image

Can someone help me, i know it is maybe a cookie, or debugging problem, but i try so much without find solution.

EDIT : When we quick in research, we have a latitude & longitude : I just want to take this data, and for this i use Webiew, ( not custom tabs because it will be open a new tabs ..)and after i set WebView Visibility to false !

If you have better solution like http or other, i dont know.. :/

Ty!


Solution

  • Regarding edit from TS, TS should probably use an API for that and then use Retrofit or something else in order to call the API.

    https://square.github.io/retrofit/

    Edit2: I extracted the calls from the website. Use this endpoint in your Retrofit instance or API call adapter in order to call the API. Look below for the request method & API endpoint.

    Request Method: GET
    Request URL: https://pkfieur.gares.io/data_pk2geo/?ligne=XXXX&pk=XXXX
    

    Where XXXX are the query strings (which you would normally type in the fields on the website), you should change those when calling the API.