2

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:

  1. Connect to the site
  2. Post login details
  3. 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.

Jurge92
  • 63
  • 1
  • 5
  • Hmm, you try to send HTTPS request but the url contains IP instead of domain. It seems HTTPS client can't verify you self-signed certificate because certificate contains server domain, but you use IP. To use HTTPS you need to have domain name or use a fake one (see http://xip.io/) – andrykonchin Nov 14 '15 at 22:07
  • http://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https – Jiang YD Jan 19 '16 at 09:16

0 Answers0