示例#1
0
        private async void CookieAwareWebClientAsync()
        {
            AccountCimage account = AppId.AccoutCimage;
            var           url     = new Uri("https://passport.csdn.net/account/login");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = "GET";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            request.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36";
            request.ContentType           = "application/x-www-form-urlencoded";
            CookieContainer cookie = new CookieContainer();

            request.CookieContainer = cookie;
            HttpWebResponse response = (HttpWebResponse)(await request.GetResponseAsync());

            cookie = request.CookieContainer;
            string sc = cookie.GetCookieHeader(url);

            string str = "";

            using (StreamReader stream = new StreamReader(response.GetResponseStream()))
            {
                str = stream.ReadToEnd();
            }


            Regex regex = new Regex(" type=\"hidden\" name=\"lt\" value=\"([\\w|\\-]+)\"");
            var   lt    = regex.Match(str).Groups[1].Value;

            regex = new Regex("type=\"hidden\" name=\"execution\" value=\"(\\w+)\"");
            var execution = regex.Match(str).Groups[1].Value;

            str = $"username={account.UserName}&password={account.Key}&lt={lt}&execution={execution}&_eventId=submit";
            //str = str.Replace("@", "%40");
            str = WebUtility.UrlEncode(str);

            request.Method = "post";
            byte[] postby = Encoding.UTF8.GetBytes(str);
        }
示例#2
0
文件: Cimage.cs 项目: zmtzawqlp/UWP
        private async void GetCookie()
        {
            AccountCimage account = AppId.AccoutCimage;
            //https://stackoverflow.com/questions/41599384/httpclient-cookie-issue
            CookieContainer cookies = new CookieContainer();

            HttpClientHandler handler = new HttpClientHandler();

            handler.CookieContainer = cookies;
            HttpClient http = new HttpClient(handler);
            var        url  = new Uri("https://passport.csdn.net/account/login");

            //https://passport.csdn.net/?service=http://write.blog.csdn.net/
            //HttpRequestHeader.
            //http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"));
            //http.DefaultRequestHeaders.AcceptEncoding.Add(StringWithQualityHeaderValue.Parse("gzip, deflate, br"));
            //http.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"));

            http.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            //http.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, br");
            http.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "zh-CN,zh;q=0.8");
            http.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");


            handler.UseCookies        = true;
            handler.AllowAutoRedirect = true;

            string str = await http.GetStringAsync(url);

            HandlCookie(handler, url);

            Regex regex = new Regex(" type=\"hidden\" name=\"lt\" value=\"([\\w|\\-]+)\"");
            var   lt    = regex.Match(str).Groups[1].Value;

            regex = new Regex("type=\"hidden\" name=\"execution\" value=\"(\\w+)\"");
            var execution = regex.Match(str).Groups[1].Value;

            str = $"username={account.UserName}&password={account.Key}&lt={lt}&execution={execution}&_eventId=submit";
            str = str.Replace("@", "%40");

            HttpContent content = new StringContent(str, Encoding.UTF8);

            //((StringContent)content).Headers
            //username=lindexi_gd%40163.com&password=Huc_3113006277&lt=LT-541277-RgRpxUYUjiMNRYpXclGBNBy0pInajL&execution=e10s1&_eventId=submit
            //username=lindexi_gd%40163.com&password=Huc_3113006277&lt=LT-541546-HzhKAKoaeftqtL6EtiCQYXrC3d716w&execution=e1s1&_eventId=submit
            str = await content.ReadAsStringAsync();

            //content=new
            content = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("username", account.UserName),//.Replace("@", "%40")),
                new KeyValuePair <string, string>("password", account.Key),
                new KeyValuePair <string, string>("lt", lt),
                new KeyValuePair <string, string>("execution", execution),
                new KeyValuePair <string, string>("_eventId", "submit")
            });
            str = await content.ReadAsStringAsync();

            str = await(await http.PostAsync(url, content)).Content.ReadAsStringAsync();
            HandlCookie(handler, url);
            //str = await http.GetStringAsync(url);
            //cookies = new CookieContainer();
            //cookies.MaxCookieSize = int.MaxValue;
            //foreach (Cookie temp in handler.CookieContainer.GetCookies(url))
            //{
            //    try
            //    {
            //        //temp.Domain = ".csdn.net";
            //        //if ("http://write.blog.csdn.net/".Contains(temp.Domain))
            //        //{
            //        //    cookies.SetCookies(new Uri("http://write.blog.csdn.net/"), temp.ToString());
            //        //}
            //    }
            //    catch
            //    {

            //    }
            //}

            //handler.CookieContainer.Add(new Uri("http://write.blog.csdn.net/"), cookies.GetCookies(new Uri("http://write.blog.csdn.net/")));

            //foreach (Cookie temp in cookies.GetCookies(new Uri("http://write.blog.csdn.net/")))
            //{
            //    handler.CookieContainer.Add();
            //}

            //var temp = handler.CookieContainer.GetCookies(url);

            //handler.CookieContainer.GetCookies()
            url = new Uri("http://write.blog.csdn.net/postlist");
            str = await http.GetStringAsync(url);

            //content=new MultipartContent();
            //((MultipartContent)content)
            content = new MultipartFormDataContent();
            ((MultipartFormDataContent)content).Headers.Add("name", "file1");
            //((MultipartFormDataContent)content)
            ((MultipartFormDataContent)content).Headers.Add("filename", "20170114120751.png");
            var stream = new StreamContent(await File.OpenStreamForReadAsync());

            ((MultipartFormDataContent)content).Add(stream);
            str = await((MultipartFormDataContent)content).ReadAsStringAsync();
            //http://write.blog.csdn.net/article/UploadImgMarkdown?parenthost=write.blog.csdn.net
            url = new Uri("http://write.blog.csdn.net/article/UploadImgMarkdown?parenthost=write.blog.csdn.net");
            HandlCookie(handler, url);

            var message = await http.PostAsync(url, content);

            if (message.StatusCode == HttpStatusCode.OK)
            {
                //str = await message.Content.ReadAsStringAsync();
                //message.Content.ReadAsStreamAsync()
                ResponseImage(message);
            }
        }