add cookie to request c#

0 thích 0 không thích
32 lượt xem
đã hỏi 9 Tháng 7, 2018 trong Lập trình VB.NET bởi nguyenthao (5,740 điểm)
public Task<string> MakeAsyncRequest(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";

            Cookie cookie = new Cookie("psortfilter", "1%24all%247Zx1Cb6rg8wQp%2FsbKRy1Rw%3D%3D");
            //"1%24all%24ti5LxTzp2%2F3wXYVLsYkp5A%3D%3D"

            //1%24all%24KCWp4ax7mh0N1UDzjrH%2BWQ%3D%3D
            request.CookieContainer = new CookieContainer();
            Uri uri = new Uri(url);
            request.CookieContainer.Add(uri, cookie);

            request.Method = WebRequestMethods.Http.Get;
            request.Timeout = 20000;
            request.Proxy = null;

            Task<WebResponse> task = Task.Factory.FromAsync(
                request.BeginGetResponse,
                asyncResult => request.EndGetResponse(asyncResult),
                (object)null);

            return task.ContinueWith(t => ReadStreamFromResponse(t.Result));
        }
Looking for an answer?  Share this question:     

Xin vui lòng đăng nhập hoặc đăng ký để trả lời câu hỏi này.

...