如何使用Java登录HTTPS网站?

时间:2011-07-14 14:42:06

标签: java html https

我希望能够使用我的用户名/密码使用java代码登录HTTPS网站(例如gmail)。 我尝试过使用我自己的代码和HTTPClient软件包(两者都允许我登录普通网站(如facebook)),但不能使用HTTPS网站。 有人能告诉我使用java登录https网站有什么必要吗? 谢谢!

示例代码:

import org.apache.http.*;

import org.apache.http.auth.*;

import org.apache.http.client.methods.*;

import org.apache.http.impl.client.*;

import org.apache.http.util.*;



public class ClientAuthentication {

public static void main(String[] args) throws Exception {

    DefaultHttpClient httpclient = new DefaultHttpClient();

    try {

        httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("email@gmail.com", "password"));
        HttpGet httpget = new HttpGet("https://www.google.com/accounts/ServiceLogin?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Fui%3Dhtml%26zy%3Dl&bsv=llya694le36z&ss=1&scc=1&ltmpl=default&ltmplcache=2&hl=en&from=logout");

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());

        if (entity != null) {

            System.out.println("Response content length: " + entity.getContentLength());

            entity.writeTo(System.out);
        }

        EntityUtils.consume(entity);

    } finally {

        httpclient.getConnectionManager().shutdown();
    }
}
}

2 个答案:

答案 0 :(得分:0)

要登录https网站,您需要检查系统时间,因为证书在2个固定日期之间有效

答案 1 :(得分:0)

您正在谈论身份验证或连接到https服务器(没有客户端身份验证)? 许多网站(如谷歌,脸书)使用OAuth或OpenID - 阅读相关内容。 如果它的其他(就像你只是使用POST)。问题是什么?你有堆栈跟踪吗?

相关问题