如何使用Java登录到HTTPS网站?

编程入门 行业动态 更新时间:2024-10-26 12:24:04
本文介绍了如何使用Java登录到HTTPS网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望能够使用我的用户名/密码使用java代码登录到HTTPS网站(如Gmail)。 我尝试过使用我自己的代码和HTTPClient包(两者都允许我登录到正常的网站(如Facebook)),但不能访问HTTPS网站。 有人能告诉我使用java登录https网站需要什么吗? Thanks!

I want to be able to log into HTTPS websites(like gmail) with my username/password using java code. I've tried using both my own code and the HTTPClient package(both allowed me to log into normal websites( like facebook)), but not into HTTPS websites. Can someone tell me what is necessary to use java to sign into a https website? Thanks!

示例代码:

Example Code:

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", "password")); HttpGet httpget = new HttpGet("www.google/accounts/ServiceLogin?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google%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(); } } }

推荐答案

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

You are talking about authentication or connecting to https server (without client auth)? Many sites (such as google, facebook) uses OAuth or OpenID - read about it. If its sth else (like you are using simply POST). What is the problem? Are you getting stacktrace?

更多推荐

如何使用Java登录到HTTPS网站?

本文发布于:2023-06-07 05:57:28,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   网站   Java   HTTPS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!