C#使用发布请求发送Cookie

时间:2018-05-01 18:24:36

标签: c# post youtube http-post

我想通过发帖请求登录youtube。我使用xNet进行HttpRequest。

我写了以下代码:

    static void Main(string[] args)
    {
         string url = "https://accounts.google.com/_/signin/sl/lookup?hl=en&_reqid=55174&rt=j";
         HttpClass httpClass = new HttpClass();;
         httpClass.PostRequestAsync(url, "username", "password");
    }


    class HttpClass {

        public async Task PostRequestAsync(string url, string account, string pass)
        {

            xNet.HttpRequest http = new xNet.HttpRequest();
            http.Cookies = new CookieDictionary();

            string type = "application/x-www-form-urlencoded;charset=utf-8";
            string query =
                "continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Ffeature%3Dsign_in_button%26action_handle_signin%3Dtrue%26app%3Ddesktop%26next%3D%252F%26hl%3Den&service=youtube&hl=en&f.req=%5B%22" + account + "%22%2C%22AEThLlxFJqXTI-dLw8jxU_Lw8c4Qtpc4DAAeEE1rpkbEUFqwwK1U86bZEzsWmZKM5IjRccPvbYTLgb0yonB3vputyMTNm-8YcGqbe_GeaB6RHFJImp_gZ-y0jFv4nduPGxM-zpJX8BahbDlIyeY2sP8-puVe3W1iwKX3rGcSFGMevHHK-ByNEUY%22%2C%5B%5D%2Cnull%2C%22TR%22%2Cnull%2Cnull%2C2%2Cfalse%2Ctrue%2C%5Bnull%2Cnull%2C%5B2%2C1%2Cnull%2C1%2C%22https%3A%2F%2Faccounts.google.com%2FServiceLogin%3Fuilel%3D3%26passive%3Dtrue%26service%3Dyoutube%26continue%3Dhttps%253A%252F%252Fwww.youtube.com%252Fsignin%253Ffeature%253Dsign_in_button%2526action_handle_signin%253Dtrue%2526app%253Ddesktop%2526next%253D%25252F%2526hl%253Den%26hl%3Den%22%2Cnull%2C%5B%5D%2C4%2C%5B%5D%2C%22GlifWebSignIn%22%5D%2C1%2C%5Bnull%2Cnull%2C%5B%5D%5D%2Cnull%2Cnull%2Cnull%2Ctrue%5D%2C%22" + account + "%22%5D&bgRequest=%5B%22identifier%22%2C%22!Pj2lPRxCiup4YICaVSxEHyFXdsNE5lECAAAAQ1IAAAAbCgAW9I8p8C1f10xg_NjCyA99rybP30APm5kBCr5B19mb-UkpwTj1ZsyybospA0TSjuUTuJeCHmkiRqKfhHxRE1CV0Yd7nifpK8VCTMNnmUMrl4-anneYlV-Bs3NQESEmJTEcxBOjvbo_tXSasO8KbZopdTxzUHm-qBGOQRTUZM4Hw6x-1HJdLoCQ2bi4FoAhbsWEt6paR0K4neYHS1kdxewjDKefWWCQ__O3C71yOjm6p0S1rjNUEM0ak9V8N2CcnIFYQ77b1B98nHCZmgMr81YtgAOF8ClSb4ZV8AiUc96rC1rvMV2RIvW54RUgsJwWHXBx0nid8tRMdUmzCTymoa-_at7qE1nJL8SMAU9WEnGOs0u2xKlBKGsjNgnqhligTDBDPnp7%22%5D&azt=AFoagUUuZ6teJ3APaa8f6ly_olQZHdGWBg%3A1525177142108&cookiesDisabled=false&deviceinfo=%5Bnull%2Cnull%2Cnull%2C%5B%5D%2Cnull%2C%22TR%22%2Cnull%2Cnull%2C%5B%5D%2C%22GlifWebSignIn%22%2Cnull%2C%5Bnull%2Cnull%2C%5B%5D%5D%5D&gmscoreversion=undefined&checkConnection=youtube%3A288%3A1&checkedDomains=youtube&pstMsg=1&";
            string html;

            html = http.Post(url, query, type).ToString();
            htmlTest(html);

            type = "application/x-www-form-urlencoded";
            query =
                account +
                "&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Ffeature%3Dsign_in_button%26action_handle_signin%3Dtrue%26app%3Ddesktop%26next%3D%252F%26hl%3Den&password=" +
                pass + "&ca=&ct=";


            http.Cookies = new CookieDictionary();
            html = http.Post(url, query, type).ToString();
            htmlTest(html);

        }

        public void htmlTest(string html)
        {
            File.WriteAllText("a.html", html);
            Process.Start("a.html");
        }

    }

回应是:

)]}' [[["er",null,null,null,["gf.rrerr",1,"https://support.google.com/accounts/answer/61416?hl\u003den"] ,null,"gf.rrerr"] ,["e",2,null,null,149] ]]

指定的链接“https://support.google.com/accounts/answer/61416?hl\u003den”表示您必须打开缓存数据。我想我需要通过帖子传递cookie但是如何通过帖子请求传递cookie?

1 个答案:

答案 0 :(得分:0)

尝试使用带代码的厨师

request.Cookies = new CookieDictionary()
      {
  {"ggg", "hhh"}
      };