我希望能够使用 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<mpl=default<mplcache=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();
}
}
}
最佳答案
要登录 https 网站,您需要检查系统时间,因为证书在 2 个固定日期之间有效
关于java - 如何用Java登录HTTPS网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6695069/