Reputation: 114
I want to create a small java application in order to communicate with an HTML server: I need to POST a form and GET results, in HTML Using Wireshark, I caught the content of the package I should send. It's something like this:
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwU[..]
[...]__EVENTVALIDATION=%2FwEWJAK9%2Fu[...]
TxTNumeroPalina=3329&ListaLocalit%C3%A0=NAPOLI&TxTViaInteresse=&TxTCAP=&BtnInviaDati=Invia+Dati
Where dots stand for other random chars. I should receive back a page containing some timelines for buses. I tried this way.
String eventTarget = "__EVENTTARGET" + "=" + "&";
String eventArgument = "__EVENTARGUMENT" + "=" + "&";
String viewState = "__VIEWSTATE" + "=" + "%2FwEPDwUJMj[...]";
String eventValidation = "__EVENTVALIDATION" + "=" + "%2FwEWJ[...]";
String eventObjects = eventTarget + eventArgument + viewState + eventValidation;
//form parameters
String numeroPalina = "TxtNumeroPalina" + "=" + "3329" + "&";
String listaLocalita = "Listalocalit%C3%A0" + "=" + "NAPOLI" + "&";
String viaInteresse = "TxtViaInteresse" + "=" + "" + "&";
String cap = "TxtCAP" + "=" + "";
String sendButton = "BtnInviaDati" + "=" + "Invia+Dati";
String locatorObjects = numeroPalina + listaLocalita + viaInteresse + cap;
String newData = URLEncoder.encode(eventTarget + eventArgument + viewState + eventValidation + locatorObjects, "UTF-8");
URL infoclickUrl = new URL("http://www.anm.it/Default.aspx");
HttpURLConnection connection = (HttpURLConnection)infoclickUrl.openConnection();
connection.setDoOutput(true);
HttpURLConnection.setFollowRedirects(true);
connection.setRequestMethod("POST");
OutputStreamWriter streamWriter = new OutputStreamWriter(connection.getOutputStream());
streamWriter.write(newData);
streamWriter.flush();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String page="";
while ((line = streamReader.readLine()) != null) {
page += line;
}
streamReader.close();
streamWriter.close();
I can never get a correct result from it! What's wrong with it?
---> Edit: In http://www.anm.it/Default.aspx you can compile a form with a certain bus stop, send it and get in return the timetable of the selected bus stop (arriving buses). So, I should receive an html page with the result of my query in it (if I send the form via browser, it works). With the code I wrote, I get back just an html page containing the initial page.
Upvotes: 0
Views: 1374
Reputation: 222
Screen Scraping 101
POST does not always translate to the full url:
URL infoclickUrl = new URL("http://www.anm.it/Default.aspx");
The port URL would be /Default.aspx HTTP X.X
Sometimes, servers use Virtual Host, so you need to append Host: anm.it in your header
Use a good tracing tool like firebug before starting to code.
HTTPUnit / HTMLUnit are perfect for the job
Upvotes: 0
Reputation: 49537
You are not handling cookies
in your java code. Try using firefox's live http header
addon and see what all data is exchanged between the browser and server. Try sending the below given cookie from your java code.
Cookie: ISAWPLB{6DC6CE50-2F43-4F96-92BC-840EFF24E706}={23855B49-8C3B-43BA-B5FB-41A85FDD9F3C}
I still believe you should use Apache HttpClient for your aplpication.
Upvotes: 1