【问题标题】:HttpURLConnection connect to my google accountHttpURLConnection 连接到我的谷歌帐户
【发布时间】:2014-01-17 15:54:30
【问题描述】:

我正在尝试使用HttpURLConnection 登录我的谷歌帐户。我不知道设置它的正确方法,我不确定它是否可以使用java.net.* 完成,否则我应该如何进行?

我的代码:

public static void main(String[] args) 
{
    try
    {
        URL myURL = new URL("https://accounts.google.com/ServiceLogin?user=***&pass=***");
        try
        {
            URLConnection myURLConnection = myURL.openConnection();
            myURLConnection.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(myURLConnection.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
            String str;
            while((str = in.readLine()) != null)
            System.out.println(str);
        }
        catch(IOException x)
        {
            System.err.format("error %s%n", x); 
        }
    }
    catch(Exception x)
    {
        System.err.format("error %s%n", x);
    }
}

代码显示我的 google 登录帐户内容页面,而不是我的帐户主页。

【问题讨论】:

  • 错误说明了什么?
  • 我没有显示错误,它显示了谷歌认证帐户页面。
  • 您从哪里得到可以在 URL 中传递用户名和密码的想法?
  • 你想达到什么目的?也许使用一些 Google API 会更好?
  • 我凭直觉尝试过,我也尝试过使用 java.net.athentication 但我在 oracle tutos 上读到它用于从服务器端设置身份验证登录。

标签: java httpurlconnection


【解决方案1】:

如果你想使用谷歌账号来认证用户,应该使用OAuth协议。对用户进行身份验证后,您可以获取用户个人资料信息,也可以根据需要访问其他 Google API。

总之,如果用户提供了有效的用户/通行证,您将获得一个身份验证令牌,指明这是一个有效的 Google 用户。

【讨论】:

    【解决方案2】:

    如果您尝试以编程方式连接到安全网站并通过登录页面,您可能需要查看 HtmlUnit Api。查看页面底部的“提交表单”示例:http://htmlunit.sourceforge.net/gettingStarted.html

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2023-03-20
      • 2018-03-14
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 2015-01-10
      • 2011-11-17
      • 1970-01-01
      相关资源
      最近更新 更多