Trying to log in another website using java and post

1
From Mendix I am trying to log into another website. This website has a login screen with three parameters. I try to write the code from java to pass the credentials to the webpage and do a login. Since my java code experience is limited I am doing something wrong because the webform does not get filled. Here is the code I use: try { //Set page url in this string. String url = "http://somewebsite.nl/Default.aspx"; java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } DefaultHttpClient httpclient = new DefaultHttpClient(); try { HttpGet httpget = new HttpGet("http://somewebsite.nl/Default.aspx"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("Login form get status: " + response.getStatusLine()); EntityUtils.consume(entity); System.out.println("Initial set of cookies:"); List<Cookie> cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } HttpPost httpost = new HttpPost("http://somewebsite.nl/SignIn.aspx"); List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("gebruikerscode", "loginname")); nvps.add(new BasicNameValuePair("wachtwoord", "password")); nvps.add(new BasicNameValuePair("klantsleutel", "somestring")); nvps.add(new BasicNameValuePair("verstuur", "verstuur")); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); response = httpclient.execute(httpost); entity = response.getEntity(); System.out.println("Login form getstatus: " + response.getStatusLine()); EntityUtils.consume(entity); System.out.println("Post logon cookies:"); cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { httpclient.getConnectionManager().shutdown(); } return true; So I have three fields I need to populate (gebruikerscode, wachtwoord and sleutel). The verstuur element is the button you would normally press after filling out this form. What am I doing wrong? Regards, Ronald
asked
1 answers
0

Hi Ronald,

Indeed I had to this before, but I don't like this way to log in. I guess we agree on that.

The way I got this working was by comparing the sent message headers from Firefox with the ones from java. In my case the cookies must be tweaked a little bit after receiving.

cookie = cookies.getValue().replaceAll(" path=/;", "").replaceAll(" HttpOnly, ", "");

passed that again with the method

method.addRequestHeader("Cookie", cookie);

Yes ugly, but as long as the web company does not provide decent authentication mechanism it is a work around.

BTW: this is with java HttpClient class. I had problems with cloudsecurity for defaultHttpClient.

answered