使用java以编程方式登录网站

时间:2016-01-13 21:13:02

标签: java http

我浏览过其他一些关于以编程方式登录网站的帖子,但他们的解决方案并没有解决我的问题。我已经习惯了JAVA一段时间,所以我非常熟悉它,但这是我第一次写任何涉及https连接的东西。我正在尝试登录link2feed.ca并从网站上提取数据。但是,我一直遇到握手失败错误。我尝试了多种不同方式连接到网站的实现,但它们都会导致相同的错误。

这是我的代码:

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();
}

以下是我收到的错误:

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)

我觉得这是一个我很想念的非常简单的问题,但我无法确定它。非常感谢所提供的任何帮助。

0 个答案:

没有答案