jeudi 28 septembre 2017

How to write a Java code to read fields from a website that requires login and uses POST request?

Need some help with fetching some data from a website.

Previously , we had following code in our application and it used to fetch the required data. We just used to read the required fields by forming a URL by passing username , password and search parameter (DEA number). The same URL (with parameters ) could also be hit from browser directly to see the results. It was a simple GET request:

{URL url = new URL(
                    "http://ift.tt/2fBBxmA"+getUsername()+"&Password="+getPassword()+"&DEA="
                            + deaNumber
                            + "&BAC=&BASC=&ExpirationDate=&Company=&Zip=&State=&PI=&MaxRows=");

Document document = parser.parse(url.toExternalForm());
// Ask the document for a list of all <sect1> tags it contains
NodeList sections = document.getElementsByTagName("DEA");
//Followed by a loop code to get each element by using sections.item(index).getFirstChild() etc.
}

Now, the website URL has got changed to following:

http://ift.tt/2xAUJYq

I am able to login to the URL with credentials , go to the search page , enter the DEA number and search. The login page comes as a pop-up once I click 'Login' link on home page. Also, the final result comes as a pop-up. This is a POST request so I am unable to form the complete URL which I could use in my code.

I am not an expert in Web Services , but I think I need a web service URL like the one mentioned in the code above. Not sure how to get that !! Even if I get the URL , I am not sure how to perform the login through Java code and search the DEA number.

Also, it would be great if I could validate the URL manually before using in Java. Let me know if there is any way.

Or, in case there is any alternate approach in Java; kindly suggest.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire