使用Cookie发送POST请求

时间:2015-01-29 01:27:50

标签: c# post cookies login webrequest

我想使用以下POST通过WebRequest发送POST请求:

POST https://olui2.fs.ml.com/ClientFederation/LoginValidateUser.asmx/ValidatePassword HTTP/1.1
Host: olui2.fs.ml.com
Connection: keep-alive
Content-Length: 76
Origin: https://olui2.fs.ml.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36
Content-Type: application/json; charset=UTF-8
Cache-Control: no-cache
X-Requested-With: XMLHttpRequest
__PageIdHeader: jclrKnUbxSsmkPo01YGJFc4O9l3kt79RUdjpUT9mB3E=
X-Bear: jclrKnUbxSsmkPo01YGJFc4O9l3kt79RUdjpUT9mB3E=
Accept: */*
Referer: https://olui2.fs.ml.com/login/ConfirmIdentity.aspx
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8
Cookie: pxv=da927b45-fda7-440d-893e-5058b60053b1; __g_u=277817800941577_1_1_1_5_1422571043310_1; __g_c=a%3A0; pxss=392613e3-6d74-4947-b707-8b6ce7a1df8e; FSDSession=true; Bear=jclrKnUbxSsmkPo01YGJFc4O9l3kt79RUdjpUT9mB3E=; NSC_pmvj2-NzNfssjmm-wt=9fea903a0000; pxsq=19

{"username":"username","rememberMe":false,"password":"mypassword","data":null}

{}用于JSON。这是我到目前为止的代码:

        //Get the variables
        string url = "http://olui.fs.ml.com/Login/Login.aspx";
        string userName = loginUN;
        string userPassword = loginPW;

        //Connection Parameters
        string method = "POST";

        HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create("https://olui2.fs.ml.com/ClientFederation/LoginValidateUser.asmx/ValidatePassword");
        webReq.Connection = "keep-alive";
        webReq.ContentLength = 76;
        webReq.UserAgent = " Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36";
        webReq.ContentType = "application/json; charset=UTF-8";
        webReq.Accept = "*/*";
        webReq.Referer = " https://olui2.fs.ml.com/login/ConfirmIdentity.aspx";

我不确定如何设置我需要用来登录的cookie。其次,我在WebRequest中找不到请求的一部分,i。即Accept-Language,Cache-Control,X-Request和所有南方部分。

我不确定的另一件大事是用户名和密码的JSON。

1 个答案:

答案 0 :(得分:2)

您在一个问题中遇到了多项挑战。也许更好地将你的问题分成多个问题。

无论如何,我将重点介绍您首次提到的cookie挑战。 HttpWebRequest组件具有CookieContainer属性。您需要创建cookie容器的实例并将其附加到该特定属性。例如:

HttpWebRequest webReq = ...
webReq.CookieContainer = new CookieContainer();
webReq.CookieContainer.Add(new Cookie("name", "value", "/", yourDomain));
// etc.

这是一个不错的博客,可能会有所帮助:http://blogs.msdn.com/b/adarshk/archive/2004/08/24/219714.aspx