I'm having big troubles connecting to a XML-based Ruby-on-Rails website.
I have tried multiple approaches, but no matter what I always end up with one or another problem.
What I need to to, is to connect to a website, POST login and password, then get the return value from the website (to see whether or not the login was successful). It is going to be an Android app at some point, but in the beginning, I just want it to work in Java.
My current not-working approach is this (apologies for hiding the IP):
public static void connect() {
@SuppressWarnings({ "deprecation", "resource" })
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://93...145/login");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("LOGIN", "name"));
nameValuePair.add(new BasicNameValuePair("PASSWORD", "password"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
HttpResponse response = httpClient.execute(httpPost);
// write response to log
System.out.println("Http Post Response:" + response.toString());
// Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// Log exception
e.printStackTrace();
} catch (IOException e) {
// Log exception
e.printStackTrace();
}
}
The website is a development server, so I get "Invalid Certificate" message when opening the page, and this makes for even bigger problems in Java. I manage to generate a "valid" certificate using the method posted her:
https://blogs.oracle.com/gc/entry/unable_to_find_valid_certification
But then I get another issue:
javax.net.ssl.SSLException: Certificate for <93...145> doesn't match common name of the certificate subject: dev-...no
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:172)
at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:61)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:140)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:114)
at org.apache.http.conn.ssl.SSLSocketFactory.verifyHostname(SSLSocketFactory.java:569)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:544)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:409)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:882)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at Tilkobling.connect(Tilkobling.java:133)
at Tilkobling.main(Tilkobling.java:33)
So basically, is there an easier way around it?
What I am trying to do is actually really simple:
- Connect to the site
- Post login details
- See the return message (which is going to be an XML with user information if the login is successful)
Any input would be greatly appreciated.