2016-04-26 4 views
2

私はクッキーを設定し、一般的な方法からクッキーを取得しようとしています。私はこの例がうまくいくのを見ましたが、自分のコードを変更して一般的な機能を維持できるような方法で問題を抱えています。HTTPクライアントクッキー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; 
    } 

コメントのコードはこれを実現するために私のtrysの一つです。

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; 
     } 
    } 

EDIT: 私もこれを試してみた:

enter image description here

しかし、それはエラーを示し、URLはokですので、私が使用する他の一般的な方法はこれですトークン。あなたはパスの一部がちょうど先に行くと、それを削除したくない場合は

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

:に

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

:行を変更するクッキーを追加するには、事前:)

答えて

0

で 感謝。

HttpResponseMessageでCookieを読み取るには、自分で解析する必要があります。ここで

は、すべてのクッキーを取得する方法である:

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

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

はそれを試してみてください、あなたはあなたがそれらを設定したときと同じ形式でそれらを見ることができます。あなたがあなた自身のためにそれらを解析するか、またはあなたのためにこれを行うクラスを見つける必要があります。

+0

こんにちは@FSDanielは、私はあなたの提案を試みたが、動作しないと私はREST簡単にアプリをテストしてみたので、要求は大丈夫です、それは「httpClient.DefaultRequestHeaders.Add(」クッキー用のI代わりに動作します"、" token = "+ tokenVal);"とその悪い要求(クッキーが間違っているとサーバーはこれを返信します) – 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(); 
      } 
     } 
    } 
関連する問題