2016-04-26 145 views
2

我想設置cookie並從一般方法獲取cookie。我看到這個例子可行,但我在改變我自己的代碼時遇到麻煩,因爲我可以保留我的一般功能。HTTP客戶端Cookie c#

CookieContainer cookies = new CookieContainer(); 
HttpClientHandler handler = new HttpClientHandler(); 
handler.CookieContainer = cookies; 

HttpClient client = new HttpClient(handler); 
HttpResponseMessage response = client.GetAsync("http://google.com").Result; 

Uri uri = new Uri("http://google.com"); 
IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri).Cast<Cookie>(); 
foreach (Cookie cookie in responseCookies) 
    Console.WriteLine(cookie.Name + ": " + cookie.Value); 

Console.ReadLine(); 

我的代碼:

public static HttpClient CreateClientASMtoken(string tokenVal) 
    { 


     var httpClient = new HttpClient 
     { 
      BaseAddress = new Uri(urlASM) 
     }; 
     httpClient.DefaultRequestHeaders.Accept.Clear(); 
     httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     //httpClient.DefaultRequestHeaders.Accept.Add(new Cookie("token", tokenVal)); 
     return httpClient; 
    } 

註釋代碼是我改掉做到這一點的一個。我使用的其他一般方法是這樣的:

public static async Task<HttpResponseMessage> PostASM(string path, object content) 
    { 
     string tokenVal = "d2GpEA5r8pwLRcOPxgaygPooldz2OZ2HUZzZ0YDPAOYCIiH4u5";    
     using (var client = CreateClientASMtoken(tokenVal)) 
     { 
      var json = JsonConvert.SerializeObject(content); 
      var serializedContent = new StringContent(json, Encoding.UTF8, "application/json"); 
      var postResponse = await client.PostAsync(path, serializedContent); 

      //string response = await postResponse.Content.ReadAsStringAsync(); 
      return postResponse; 
     } 
    } 

編輯: 我已經試過這太:

enter image description here

但它顯示了一個錯誤,網址爲OK,所以是令牌。 感謝提前:)

回答

0

要添加一個cookie更改行:

//httpClient.DefaultRequestHeaders.Accept.Add(new Cookie("token", tokenVal)); 

到:

httpClient.DefaultRequestHeaders.Add("Set-Cookie", "token=test; path=/"); 

如果你不想路徑部分先走一步,將其刪除。

如果你想從HttpResponseMessage讀出cookie,你需要自己處理解析。

這是一種方式來獲得所有cookie:

 HttpClient client = CreateClientASMtoken(""); 
     HttpResponseMessage response = client.GetAsync("http://localhost").Result; 

     IEnumerable<string> rawCookies = response.Headers.GetValues("Set-Cookie"); 

試試吧,你會看到他們在相同的格式,當你將它們設置爲。如前所述,您需要爲自己解析它們,或者找一個班級爲您做這件事。

+0

嗨@FSDaniel我想你的建議,但不工作,要求是可以的,因爲我受夠了這種高枕無憂的應用程序進行測試和它的作品我代替「httpClient.DefaultRequestHeaders.Add(」曲奇「 」標記=「 + tokenVal);」和一個不好的請求(如果cookie錯誤,服務器會回覆它) – Antoine

0

我找到了一種輕鬆解決問題的方法。感謝那些貢獻的人。

public static async Task<string> GetASM(string path) 
    { 
     string tokenVal = "d2GpEA5r8pwLRcOPxgaygPooldz2OZ2HUZzZ0YDPAOYCIiH4u5"; 
     Uri uriASM = new Uri(urlASM); 
     var cookieContainer = new CookieContainer(); 
     using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer }) 
     using (var client = new HttpClient(handler) { BaseAddress = uriASM }) 
     { 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
      { 
       cookieContainer.Add(uriASM, new Cookie("token", tokenVal)); 
       var getResponse = await client.GetAsync(path); 
       return await getResponse.Content.ReadAsStringAsync(); 
      } 
     } 
    }