Using CookieContainer with WebClient class

后端 未结 5 1916
遇见更好的自我
遇见更好的自我 2020-11-22 02:07

I\'ve previously used a CookieContainer with HttpWebRequest and HttpWebResponse sessions, but now, I want to use it with a WebClient. As far as I understand, there is no bui

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 02:32

     WebClient wb = new WebClient();
     wb.Headers.Add(HttpRequestHeader.Cookie, "somecookie");
    

    From Comments

    How do you format the name and value of the cookie in place of "somecookie" ?

    wb.Headers.Add(HttpRequestHeader.Cookie, "cookiename=cookievalue"); 
    

    For multiple cookies:

    wb.Headers.Add(HttpRequestHeader.Cookie, 
                  "cookiename1=cookievalue1;" +
                  "cookiename2=cookievalue2");
    

提交回复
热议问题