如何使用 C# 的 HTTP POST 请求登录 linkedin

问题描述 投票:0回答:4

我正在开发一个简单的 Windows 窗体程序,该程序从“文本框”获取用户名和密码,然后在“消息框”中显示我的链接名称。

我想使用“HttpWebRequest”或使用任何方法将我的 POST 请求发送到 Linked-in 来完成代码,然后我可以获得响应并找到我的名字以将其显示在“消息框”中。

我熟悉创建“GET”请求,也做了一些“POST”请求,但在这种情况下,我不知道如何使用 POST 请求发送“txt_UserName.Text”和“txt_Password”以及如何发送我收到回复。

当我尝试登录时,我尝试使用 Fiddler 从 linkedin 捕获 POST 请求(=POST),但当我看到它们的标头时,它捕获了超过 4 个请求,它看起来像是一个 GET 请求,这是一个示例:

GET /nhome/?trk= HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

并且它们都有多个 cookie 值。

这是我的 POST 请求代码:

    public void SubmitData()
    {
        try
        {              
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create("http://www.linkedin.com");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            //Content Length
            request.ContentLength = byteArray.Length;

            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();

            // Get the response.
            WebResponse response = request.GetResponse();
            dataStream = response.GetResponseStream();

            StreamReader sr = new StreamReader(dataStream);
            MessageBox.Show(sr.ReadToEnd());

            sr.Close();
            dataStream.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }
    }

现在我唯一想知道的是,如何将我的用户名和密码作为登录链接的值发送?


编辑:

下面是我的第二次尝试,没关系,我现在可以在 postData 中发送用户和密码,我可以存储 Cookie 并检索它。但有两个问题: 1-我怎样才能确保登录完成并且没有失败 2-如果登录完成,我想知道从个人资料中获取我的名字的第二步是什么,是提出了另一个请求还是什么?

  private void button1_Click(object sender, EventArgs e)
    {
        PostMessage();
    }


    private void PostMessage()
    {
        try { 

        // POST Data and the POST uri
        string    postData = "isJsEnabled=true&source_app=&session_key=" + textBox1.Text + "&session_password=" + textBox2.Text + "&signin=Sign+In&session_redirect=";
        string    uri = "https://www.linkedin.com/uas/login-submit";

        // Encoding the POST Data
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Create the POST Request
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(uri);

        //POST Parameters (Method and etc.)
        WebReq.Method = "POST";
        WebReq.ContentType = "application/x-www-form-urlencoded";
        WebReq.ContentLength = byteArray.Length;

        // Set the POST Request Cookies
        var cookieContainer = new CookieContainer();
        WebReq.CookieContainer = cookieContainer;

        // Get the request stream.
        Stream dataStream = WebReq.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();


        // Get the response.
        HttpWebResponse response = (HttpWebResponse)WebReq.GetResponse();
        dataStream = response.GetResponseStream();

        StreamReader sr = new StreamReader(dataStream);
        //MessageBox.Show(sr.ReadToEnd());
        sr.Close();
        dataStream.Close();

        if (response.StatusCode != HttpStatusCode.OK)
        {
            MessageBox.Show(" Error: " + response.StatusDescription);
            response.Close();
        }

        foreach (Cookie cook in response.Cookies)
        {
            MessageBox.Show(cook.Name + " " + cook.Value);
        }

            }

        catch (Exception ex)
        {
            MessageBox.Show("POST Message Error: " + ex.Message);
        }

    }
c# .net http post fiddler
4个回答
0
投票

我在登录时使用了 Fiddler,发现了一个对 https://www.linkedin.com/uas/login-submit 的请求,其中包含用户名和密码。找到了?现在,如果您想从 HTTP 请求的角度完全看待它,您将必须弄清楚如何使用浏览器发送和接收的其他请求和响应来生成 post data/cookie 标头中的其他数据在此特定请求之前的站点(该信息应该在那里)。我认为这会引导您完成您需要做的事情,但是还有一些工作要做!


0
投票

您将需要比您想象的更多的内容才能登录,这里有关于如何执行此操作的良好文档,您将需要身份验证令牌等,这是因为与其他服务(例如谷歌)一样,它们正在使用 oauth2 来保护应用程序等。

oauth 通过发行令牌和刷新令牌来工作,有一点学习曲线,但并不是特别困难。

基本上会发生以下情况

  1. 您通过链接注册您的申请,他们会给您一个 客户秘密。
  2. 您将此代码传递到您的应用程序中链接 他们将生成一个身份验证屏幕,表明该应用程序是 请求许可。
  3. 然后您批准此操作,它将给您一个访问令牌
  4. 然后您使用访问令牌登录(linkedin 上的访问令牌有效期为 60 天,此时您必须刷新它们)。

从好的方面来说,API 中的链接非常简单,一旦授权,您将能够很容易地获得东西。所有这些都在分步提供的链接中详细说明。

顺便说一句,还有一个 nuget 包可以让您访问个人资料信息。

尝试

Install-Package LinkedIn

我应该指出,上面的 nuget 包为您提供了一个登录提供程序,可以帮助您进行身份验证(如果您不想自行推出)。

在您的评论后添加。

如果您只想知道如何发送发布请求,这里有一段通用代码可以做到这一点:

string url = "www.foo.bar.com";

using (var webClient= new WebClient())
{
    var data = new NameValueCollection();
    data["username"] = "<yourusername>";
    data["password"] = "<yourPassword>";

    var response = webClient.UploadValues(url, "POST", data);
}

注意:因为它是一个 Web URI,所以您应该在此处的方法参数中使用 POST。


0
投票

您遇到的问题是 Linkedin 在登录会话中使用重定向,并且很难捕获。 因此,在登录会话中,您需要以某种方式进行编码,使其重定向到 https://www.linkedin.com/nhome/?trk= 这提供了用户页面访问权限。

我也在测试这个,但是通常没能弄清楚这部分 httpWebRequest.AllowAutoRedirect = true; 应该可以解决问题,但在这种情况下它不起作用。

因此,如果您找到解决方案,请告诉我,如果我找到了,也会发布它。


0
投票

检查我在 GitHub 上创建的这个要点,其中包含示例代码,使 C# .NET 应用程序使用 LinkedIn 登录用户

使用 C# .NET 应用程序和 LinkedIn OpenID Connect 登录用户 https://gist.github.com/doriandres/e3df57e1cd9a3498e797a566df2a940b

© www.soinside.com 2019 - 2024. All rights reserved.