使用HTTP POST请求用C#我如何可以登录到LinkedIn [英] How can i login to linkedin using HTTP POST Request with C#

查看:142
本文介绍了使用HTTP POST请求用C#我如何可以登录到LinkedIn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个简单的Windows窗体程序,需要用户名和密码,从一个文本框,那么它​​显示我的链接的名称在消息框。

我要完成的code与利用的HttpWebRequest,或使用任何方法来发送我的POST请求链接,在那我可以得到响应,找到我的名字,以示其在消息框

我熟悉创建一个GET请求,也是我做了一些POST请求,但在这种情况下,我不知道我怎么能送我的txt_UserName.Text和txt_Password使用POST请求和我怎样才能收到响应。

我试着使用招捕获POST请求(= POST)从LinkedIn当我尝试登录,但它抓住了4个以上的请求,当我看到他们的它看起来像一个GET请求的头,这是一个例子:

  GET / nhome /?TRK = HTTP / 1.1
接受:text / html的,是application / xhtml + xml的,应用程序/ XML; Q = 0.9,* / *; Q = 0.8
 

和所有的人都有多个cookie的值。

这是我的POST请求code:

 公共无效SubmitData()
    {
        尝试
        {
            字符串POSTDATA =这是一个测试,帖子这个字符串到Web服务器。
            字节[]的字节数组= Encoding.UTF8.GetBytes(POSTDATA);

            //创建使用,它可以接收后的URL的请求。
            WebRequest的请求= WebRequest.Create(http://www.linkedin.com);
            //请求POST的方法属性设置。
            request.Method =POST;
            //设置的WebRequest的CONTENTLENGTH财产。
            request.ContentLength = byteArray.Length;
            //内容长度
            request.ContentLength = byteArray.Length;

            //获取请求流。
            流式传输的数据流= request.GetRequestStream();
            //将数据写入请求流。
            dataStream.Write(字节数组,0,byteArray.Length);
            //关闭Stream对象。
            dataStream.Close();

            //得到响应。
            WebResponse的响应= request.GetResponse();
            数据流= response.GetResponseStream();

            StreamReader的SR =新的StreamReader(数据流);
            的MessageBox.show(sr.ReadToEnd());

            sr.Close();
            dataStream.Close();
        }
        赶上(例外前)
        {
            的MessageBox.show(错误:+ ex.Message);
        }
    }
 

现在我唯一想知道,我怎么能送我的用户名和密码作为一个值来登录链接吗?


编辑:

下面是我的第二次​​尝试,它的确定,我现在可以把用户和密码POSTDATA,我可以存储Cookie和retrive它。但有两个问题: 1 - 我怎样才能确保登录完成,而不是失败 2 - 如果登录完成我想知道什么是第二步,从配置文件让我的名字,是不是做了另一个请求,或者是什么?

 私人无效的button1_Click(对象发件人,EventArgs的)
    {
        PostMessage的();
    }


    私人无效PostMessage的()
    {
        尝试 {

        // POST数据和POST URI
        字符串POSTDATA =isJsEnabled =真放; source_app =安培; session_key可以=+ textBox1.Text +&放大器; session_password =+ textBox2.Text +&放大器;的登入=符号+在与放大器; session_redirect =;
        字符串URI =htt​​ps://www.linkedin.com/uas/login-submit;

        //编码POST数据
        字节[]的字节数组= Encoding.UTF8.GetBytes(POSTDATA);

        //创建POST请求
        HttpWebRequest的WebReq =(HttpWebRequest的)WebRequest.Create(URI);

        // POST参数(方法等)
        WebReq.Method =POST;
        WebReq.Co​​ntentType =应用/的X WWW的形式urlen codeD;
        WebReq.Co​​ntentLength = byteArray.Length;

        //设置POST请求饼干
        VAR的CookieContainer =新的CookieContainer();
        WebReq.Co​​okieContainer =的CookieContainer;

        //获取请求流。
        流式传输的数据流= WebReq.GetRequestStream();
        //将数据写入请求流。
        dataStream.Write(字节数组,0,byteArray.Length);
        //关闭Stream对象。
        dataStream.Close();


        //得到响应。
        HttpWebResponse响应=(HttpWebResponse)WebReq.GetResponse();
        数据流= response.GetResponseStream();

        StreamReader的SR =新的StreamReader(数据流);
        //MessageBox.Show(sr.ReadToEnd());
        sr.Close();
        dataStream.Close();

        如果(response.Status code!=的HTTPStatus code.OK)
        {
            的MessageBox.show(错误:+ response.StatusDescription);
            response.Close();
        }

        的foreach(Cookie的厨师response.Cookies)
        {
            的MessageBox.show(cook.Name ++ cook.Value);
        }

            }

        赶上(例外前)
        {
            的MessageBox.show(POST消息错误:+ ex.Message);
        }

    }
 

解决方案

这个问题你反对的是,LinkedIn使用登录会话中重定向并differcult赶上。 所以,在某种程度上,你需要code中的登录会话,它重定向到 HTTPS中:// WWW .linkedin.com / nhome /?TRK = 这为用户提供了页面的访问。

我也有这个测试,但是没能弄清楚这部分出来,通常       httpWebRequest.AllowAutoRedirect = TRUE; 应该做的伎俩,但不是在这种情况下,这是行不通的。

所以,如果你找到解决方案让我知道,如果我发现将它张贴也。

I am working on a simple Windows Forms program that take a username and password from a "Textbox" then it show my linked-in name in a "Messagebox".

I want to accomplish the code with the using of "HttpWebRequest" or using any method to send my POST request to Linked-in then i can get the response and find my name to shown it in a "Messagebox".

I am familiar with creating a "GET" Request and also i made some "POST" Requests but in this case i didn't know how can i send my "txt_UserName.Text" and "txt_Password" with the POST Request and how can i receives the Response.

I tried to using Fiddler to capture POST request (=POST) from linkedin when i try to login but it captures more than 4 requests when i see the header of them it seem like a GET Request this is an example of one:

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

and all of them have a multiple cookies values.

This is my POST request code:

    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);
        }
    }

Now the only thing i wish to know, how can i send my username and password as a values to login to linked-in?


Edit:

Below is my second try, it's ok, i can now send the User and Password in postData and i can store the Cookies and retrive it. but there are two issues: 1- how can i make sure that the login is accomplished and not failed 2- if the login is accomplished i want to know what is the second step to get my name from profile, is it made another request or what ?

  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);
        }

    }

解决方案

The problem you up against is that Linkedin uses a redirect within the login session and is differcult to catch. So somehow within the login session you need to code that it redirects to the https://www.linkedin.com/nhome/?trk= this provides the user page access.

I am also testing with this, however did not manage to figure this part out, normally httpWebRequest.AllowAutoRedirect = true; should do the trick but not in this case it does not work.

So if you find the solution let me know, if I find will post it also.

这篇关于使用HTTP POST请求用C#我如何可以登录到LinkedIn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆