0

I've browsed through several of the other posts about programatically logging into websites, but their solutions have not solved my issue. I've been fiddling around with JAVA for a while so am pretty familiar with it, but this is my first time writing anything that deals with https connections. I am attempting to login to link2feed.ca and pull data from the website. However, I keep running into a handshake failure error. I've tried multiple implementations of different ways to connect to a site, but they all result in the same error.

Here is my code:

URI LINK_2_FEED_LOGIN_URL = new URI("https://portal.link2feed.ca/login_check");

BasicCookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient httpclient = HttpClients.custom()
    .setDefaultCookieStore(cookieStore)
    .build();

try {

   HttpGet httpget = new HttpGet(LINK_2_FEED_LOGIN_URL);
   CloseableHttpResponse response1 = httpclient.execute(httpget);
   try {
        HttpEntity entity = response1.getEntity();

        System.out.println("Login form get: " + response1.getStatusLine());
        EntityUtils.consume(entity);

        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = cookieStore.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 {
        response1.close();
   }

   HttpUriRequest login = RequestBuilder.post()
           .setUri(LINK_2_FEED_LOGIN_URL)
           .addParameter("login_email", USER)
           .addParameter("login_password", PASSWORD)
           .build();
   CloseableHttpResponse response2 = httpclient.execute(login);
   try {
        HttpEntity entity = response2.getEntity();

        System.out.println("Login form get: " + response2.getStatusLine());
            EntityUtils.consume(entity);

        System.out.println("Post logon cookies:");
        List<Cookie> cookies = cookieStore.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 {
        response2.close();
    }
} finally {
    httpclient.close();
}

Here is the error I receive:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1959)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1077)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at myproject.project.getURLdata(project.java:69)
at myproject.project.<init>(project.java:52)
at myproject.project.main(project.java:155)

I feel like it is a very simple problem I'm missing, but I have not been able to pinpoint it. Any assistance offered would be greatly appreciated.

JavaTheHut
  • 21
  • 2
  • 2
    Is that the ***complete*** stack trace? Including all "caused by" sections? – Jim Garrison Jan 13 '16 at 21:14
  • You first need to challenge the server, otherwise the server will not let you log in. That will return and then you can log in. Before logging in, try to get a secured page, then when that returns, you can log in. – purring pigeon Jan 13 '16 at 21:17
  • Yes this is the complete trace. Apologies I did not put in the whole code previously I accidentally omitted the preceding piece it is now complete. – JavaTheHut Jan 13 '16 at 21:19
  • This looks like it might be a duplicate of http://stackoverflow.com/questions/6353849/received-fatal-alert-handshake-failure-through-sslhandshakeexception. At the very least, the answer there may be helpful. – neuronaut Jan 13 '16 at 21:21
  • Right off the bat, the URL you have in `LINK_2_FEED_LOGIN_URL` is the one to display the login form, not the form's submit URL. The form contains `
    `, so the submit URI is `/login_check` (host-relative). That's probably not causing the exception however.
    – Jim Garrison Jan 13 '16 at 21:29
  • One critical piece of data that's missing is how you've configured JSSE for SSL/TLS. The exception points to a configuration problem in the SSL layer. – Jim Garrison Jan 13 '16 at 21:31
  • Nice, I was unsure about that /login_check url, thanks for clearing that up at least. – JavaTheHut Jan 13 '16 at 21:40
  • I don't think I've done that if it is required. The code examples I've seen did not show any other configuration code. But perhaps I missed it. I will relook at the examples. – JavaTheHut Jan 13 '16 at 22:18
  • I've looked into SSL settings and could only identify certificate authentication as something I missed. The site uses a self signed certificate but since my application will only be connecting this site I added code to accept all certificates and still run into the same issue. – JavaTheHut Jan 19 '16 at 18:07

0 Answers0