public async Task GetAsync_ReceiveMultipleSetCookieHeaders_CookieAdded() { await LoopbackServer.CreateServerAsync(async (server, url) => { HttpClientHandler handler = CreateHttpClientHandler(); using (HttpClient client = CreateHttpClient(handler)) { Task <HttpResponseMessage> getResponseTask = client.GetAsync(url); Task <List <string> > serverTask = server.AcceptConnectionSendResponseAndCloseAsync( HttpStatusCode.OK, $"Set-Cookie: A=1; Path=/\r\n" + $"Set-Cookie : B=2; Path=/\r\n" + // space before colon to verify header is trimmed and recognized $"Set-Cookie: C=3; Path=/\r\n", "Hello world!"); await TestHelper.WhenAllCompletedOrAnyFailed(getResponseTask, serverTask); CookieCollection collection = handler.CookieContainer.GetCookies(url); Assert.Equal(3, collection.Count); // Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie> Cookie[] cookies = new Cookie[3]; collection.CopyTo(cookies, 0); Assert.Contains(cookies, c => c.Name == "A" && c.Value == "1"); Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2"); Assert.Contains(cookies, c => c.Name == "C" && c.Value == "3"); } }); }
public async Task GetAsync_ReceiveMultipleSetCookieHeaders_CookieAdded() { await LoopbackServer.CreateServerAsync(async (server, url) => { HttpClientHandler handler = CreateHttpClientHandler(); using (HttpClient client = new HttpClient(handler)) { Task <HttpResponseMessage> getResponseTask = client.GetAsync(url); Task <List <string> > serverTask = LoopbackServer.ReadRequestAndSendResponseAsync(server, $"HTTP/1.1 200 OK\r\n" + $"Date: {DateTimeOffset.UtcNow:R}\r\n" + $"Set-Cookie: A=1; Path=/\r\n" + $"Set-Cookie : B=2; Path=/\r\n" + // space before colon to verify header is trimmed and recognized $"Set-Cookie: C=3; Path=/\r\n" + $"Content-Length: {s_simpleContent.Length}\r\n" + $"\r\n" + $"{s_simpleContent}"); await TestHelper.WhenAllCompletedOrAnyFailed(getResponseTask, serverTask); CookieCollection collection = handler.CookieContainer.GetCookies(url); Assert.Equal(3, collection.Count); // Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie> Cookie[] cookies = new Cookie[3]; collection.CopyTo(cookies, 0); Assert.Contains(cookies, c => c.Name == "A" && c.Value == "1"); Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2"); Assert.Contains(cookies, c => c.Name == "C" && c.Value == "3"); } }); }
public async Task GetAsync_ReceiveInvalidSetCookieHeader_ValidCookiesAdded() { await LoopbackServerFactory.CreateServerAsync(async (server, url) => { HttpClientHandler handler = CreateHttpClientHandler(); using (HttpClient client = CreateHttpClient(handler)) { Task <HttpResponseMessage> getResponseTask = client.GetAsync(url); Task <HttpRequestData> serverTask = server.HandleRequestAsync( HttpStatusCode.OK, new HttpHeaderData[] { new HttpHeaderData("Set-Cookie", "A=1; Path=/;Expires=asdfsadgads"), // invalid Expires new HttpHeaderData("Set-Cookie", "B=2; Path=/"), new HttpHeaderData("Set-Cookie", "C=3; Path=/") }, s_simpleContent); await TestHelper.WhenAllCompletedOrAnyFailed(getResponseTask, serverTask); CookieCollection collection = handler.CookieContainer.GetCookies(url); Assert.Equal(2, collection.Count); // Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie> Cookie[] cookies = new Cookie[3]; collection.CopyTo(cookies, 0); Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2"); Assert.Contains(cookies, c => c.Name == "C" && c.Value == "3"); } }); }
public async Task GetAsync_ReceiveInvalidSetCookieHeader_ValidCookiesAdded() { if (IsNetfxHandler) { // NetfxHandler incorrectly only processes one valid cookie return; } await LoopbackServer.CreateServerAsync(async (server, url) => { HttpClientHandler handler = CreateHttpClientHandler(); using (HttpClient client = new HttpClient(handler)) { Task <HttpResponseMessage> getResponseTask = client.GetAsync(url); Task <List <string> > serverTask = server.AcceptConnectionSendResponseAndCloseAsync( HttpStatusCode.OK, $"Set-Cookie: A=1; Path=/;Expires=asdfsadgads\r\n" + // invalid Expires $"Set-Cookie: B=2; Path=/\r\n" + $"Set-Cookie: C=3; Path=/\r\n", s_simpleContent); await TestHelper.WhenAllCompletedOrAnyFailed(getResponseTask, serverTask); CookieCollection collection = handler.CookieContainer.GetCookies(url); Assert.Equal(2, collection.Count); // Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie> Cookie[] cookies = new Cookie[3]; collection.CopyTo(cookies, 0); Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2"); Assert.Contains(cookies, c => c.Name == "C" && c.Value == "3"); } }); }
public async Task GetAsyncWithBasicAuth_ReceiveSetCookie_CookieSent() { if (UseVersion == HttpVersion30) { // [ActiveIssue("https://github.com/dotnet/runtime/issues/56870")] return; } if (IsWinHttpHandler) { // Issue https://github.com/dotnet/runtime/issues/24979 // WinHttpHandler does not process the cookie. return; } await LoopbackServerFactory.CreateClientAndServerAsync(async url => { HttpClientHandler handler = CreateHttpClientHandler(); handler.Credentials = new NetworkCredential("user", "pass"); using (HttpClient client = CreateHttpClient(handler)) { await client.GetAsync(url); CookieCollection collection = handler.CookieContainer.GetCookies(url); Assert.Equal(2, collection.Count); // Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie> Cookie[] cookies = new Cookie[2]; collection.CopyTo(cookies, 0); Assert.Contains(cookies, c => c.Name == "A" && c.Value == "1"); Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2"); } }, async server => { HttpRequestData requestData1 = await server.HandleRequestAsync( HttpStatusCode.Unauthorized, new HttpHeaderData[] { new HttpHeaderData("WWW-Authenticate", "Basic realm=\"WallyWorld\""), new HttpHeaderData("Set-Cookie", "A=1; Path=/") }); Assert.Equal(0, requestData1.GetHeaderValueCount("Cookie")); HttpRequestData requestData2 = await server.HandleRequestAsync( HttpStatusCode.OK, new HttpHeaderData[] { new HttpHeaderData("Set-Cookie", "B=2; Path=/") }, s_simpleContent); Assert.Equal("A=1", requestData2.GetSingleHeaderValue("Cookie")); }); }
public static List <Cookie> ToList(this CookieCollection cookies) { var cookieArray = new Cookie[cookies.Count]; cookies.CopyTo(cookieArray, 0); return(cookieArray.ToList()); }
public void CopyTo() { Array a = Array.CreateInstance(typeof(Cookie), 3); col.CopyTo(a, 0); Assert.AreEqual(a.GetValue(0), col [0], "#1"); Assert.AreEqual(a.GetValue(1), col [1], "#2"); Assert.AreEqual(a.GetValue(2), col [2], "#3"); }
public async Task GetAsyncWithRedirect_ReceiveSetCookie_CookieSent() { if (UseVersion == HttpVersion30) { // [ActiveIssue("https://github.com/dotnet/runtime/issues/56870")] return; } const string path1 = "/foo"; const string path2 = "/bar"; await LoopbackServerFactory.CreateClientAndServerAsync(async url => { Uri url1 = new Uri(url, path1); HttpClientHandler handler = CreateHttpClientHandler(); using (HttpClient client = CreateHttpClient(handler)) { client.DefaultRequestHeaders.ConnectionClose = true; // to avoid issues with connection pooling await client.GetAsync(url1); CookieCollection collection = handler.CookieContainer.GetCookies(url); Assert.Equal(2, collection.Count); // Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie> Cookie[] cookies = new Cookie[2]; collection.CopyTo(cookies, 0); Assert.Contains(cookies, c => c.Name == "A" && c.Value == "1"); Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2"); } }, async server => { HttpRequestData requestData1 = await server.HandleRequestAsync( HttpStatusCode.Found, new HttpHeaderData[] { new HttpHeaderData("Location", $"{path2}"), new HttpHeaderData("Set-Cookie", "A=1; Path=/") }); Assert.Equal(0, requestData1.GetHeaderValueCount("Cookie")); HttpRequestData requestData2 = await server.HandleRequestAsync( HttpStatusCode.OK, new HttpHeaderData[] { new HttpHeaderData("Set-Cookie", "B=2; Path=/") }, s_simpleContent); Assert.Equal("A=1", requestData2.GetSingleHeaderValue("Cookie")); }); }
public async Task GetAsyncWithBasicAuth_ReceiveSetCookie_CookieSent() { if (IsWinHttpHandler) { // Issue #26986 // WinHttpHandler does not process the cookie. return; } await LoopbackServer.CreateClientAndServerAsync(async url => { HttpClientHandler handler = CreateHttpClientHandler(); handler.Credentials = new NetworkCredential("user", "pass"); using (HttpClient client = new HttpClient(handler)) { await client.GetAsync(url); CookieCollection collection = handler.CookieContainer.GetCookies(url); Assert.Equal(2, collection.Count); // Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie> Cookie[] cookies = new Cookie[2]; collection.CopyTo(cookies, 0); Assert.Contains(cookies, c => c.Name == "A" && c.Value == "1"); Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2"); } }, async server => { await server.AcceptConnectionAsync(async connection => { List <string> request1Lines = await connection.ReadRequestHeaderAndSendResponseAsync( HttpStatusCode.Unauthorized, $"WWW-Authenticate: Basic realm=\"WallyWorld\"\r\nSet-Cookie: A=1; Path=/\r\n"); Assert.Equal(0, request1Lines.Count(s => s.StartsWith("Cookie:"))); List <string> request2Lines = await connection.ReadRequestHeaderAndSendResponseAsync( HttpStatusCode.OK, $"Set-Cookie: B=2; Path=/\r\n", s_simpleContent); Assert.Contains($"Cookie: A=1", request2Lines); Assert.Equal(1, request2Lines.Count(s => s.StartsWith("Cookie:"))); }); }); }
public async Task GetAsyncWithBasicAuth_ReceiveSetCookie_CookieSent() { if (IsWinHttpHandler) { // Issue #26986 // WinHttpHandler does not process the cookie. return; } await LoopbackServer.CreateServerAndClientAsync(async url => { HttpClientHandler handler = CreateHttpClientHandler(); handler.Credentials = new NetworkCredential("user", "pass"); using (HttpClient client = new HttpClient(handler)) { await client.GetAsync(url); CookieCollection collection = handler.CookieContainer.GetCookies(url); Assert.Equal(2, collection.Count); // Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie> Cookie[] cookies = new Cookie[2]; collection.CopyTo(cookies, 0); Assert.Contains(cookies, c => c.Name == "A" && c.Value == "1"); Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2"); } }, async server => { await LoopbackServer.AcceptSocketAsync(server, async(_, stream, reader, writer) => { List <string> request1Lines = await LoopbackServer.ReadWriteAcceptedAsync(server, reader, writer, $"HTTP/1.1 401 Unauthorized\r\nContent-Length: 0\r\nWWW-Authenticate: Basic realm=\"WallyWorld\"\r\nSet-Cookie: A=1; Path=/\r\n\r\n"); Assert.Equal(0, request1Lines.Count(s => s.StartsWith("Cookie:"))); List <string> request2Lines = await LoopbackServer.ReadWriteAcceptedAsync(server, reader, writer, $"HTTP/1.1 200 OK\r\nContent-Length: {s_simpleContent.Length}\r\nSet-Cookie: B=2; Path=/\r\n\r\n{s_simpleContent}"); Assert.Contains($"Cookie: A=1", request2Lines); Assert.Equal(1, request2Lines.Count(s => s.StartsWith("Cookie:"))); return(null); }); }); }
private CookieContainer copyContainer(CookieContainer from, String url) { CookieCollection fromCookieCollection = from.GetCookies(new Uri(url)); CookieCollection toCookieCollection = new CookieCollection(); Cookie[] s = new Cookie[fromCookieCollection.Count]; fromCookieCollection.CopyTo(s, 0); for (int i = 0; i < s.Length; ++i) { toCookieCollection.Add(s[i]); } CookieContainer ret = new CookieContainer(); ret.Add(toCookieCollection); return(ret); }
/// <summary> /// This method is used to get the specified cookie name exists on the server. /// </summary> /// <param name="cookieIndex">The cookie index which name to be returned.</param> /// <param name="cookieContainer">The cookie container which contains the specified cookie.</param> /// <returns>The specified cookie name exists on the server.</returns> protected string GetCookieName(int cookieIndex, CookieContainer cookieContainer) { string cookieName = string.Empty; CookieCollection cookieCollection = cookieContainer.GetCookies(new Uri(this.GetFormsAuthenticationServiceUrl())); if (cookieCollection != null) { if (cookieCollection.Count != 0) { Cookie[] cookies = new Cookie[cookieCollection.Count]; cookieCollection.CopyTo(cookies, 0); cookieName = cookies[cookieIndex].Name; } } return(cookieName); }
public async Task GetAsyncWithRedirect_ReceiveSetCookie_CookieSent() { const string path1 = "/foo"; const string path2 = "/bar"; await LoopbackServer.CreateClientAndServerAsync(async url => { Uri url1 = new Uri(url, path1); HttpClientHandler handler = CreateHttpClientHandler(); using (HttpClient client = new HttpClient(handler)) { client.DefaultRequestHeaders.ConnectionClose = true; // to avoid issues with connection pooling await client.GetAsync(url1); CookieCollection collection = handler.CookieContainer.GetCookies(url); Assert.Equal(2, collection.Count); // Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie> Cookie[] cookies = new Cookie[2]; collection.CopyTo(cookies, 0); Assert.Contains(cookies, c => c.Name == "A" && c.Value == "1"); Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2"); } }, async server => { List <string> request1Lines = await server.AcceptConnectionSendResponseAndCloseAsync( HttpStatusCode.Found, $"Location: {path2}\r\nSet-Cookie: A=1; Path=/\r\n"); Assert.Equal(0, request1Lines.Count(s => s.StartsWith("Cookie:"))); List <string> request2Lines = await server.AcceptConnectionSendResponseAndCloseAsync( HttpStatusCode.OK, $"Set-Cookie: B=2; Path=/\r\n", s_simpleContent); Assert.Contains($"Cookie: A=1", request2Lines); Assert.Equal(1, request2Lines.Count(s => s.StartsWith("Cookie:"))); }); }
public async Task GetAsyncWithRedirect_ReceiveSetCookie_CookieSent() { const string path1 = "/foo"; const string path2 = "/bar"; await LoopbackServer.CreateServerAndClientAsync(async url => { Uri url1 = new Uri(url, path1); HttpClientHandler handler = CreateHttpClientHandler(); using (HttpClient client = new HttpClient(handler)) { await client.GetAsync(url1); CookieCollection collection = handler.CookieContainer.GetCookies(url); Assert.Equal(2, collection.Count); // Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie> Cookie[] cookies = new Cookie[2]; collection.CopyTo(cookies, 0); Assert.Contains(cookies, c => c.Name == "A" && c.Value == "1"); Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2"); } }, async server => { List <string> request1Lines = await LoopbackServer.ReadRequestAndSendResponseAsync(server, $"HTTP/1.1 302 Found\r\nContent-Length: 0\r\nLocation: {path2}\r\nSet-Cookie: A=1; Path=/\r\nConnection: close\r\n\r\n"); Assert.Equal(0, request1Lines.Count(s => s.StartsWith("Cookie:"))); List <string> request2Lines = await LoopbackServer.ReadRequestAndSendResponseAsync(server, $"HTTP/1.1 200 OK\r\nContent-Length: {s_simpleContent.Length}\r\nSet-Cookie: B=2; Path=/\r\n\r\n{s_simpleContent}"); Assert.Contains($"Cookie: A=1", request2Lines); Assert.Equal(1, request2Lines.Count(s => s.StartsWith("Cookie:"))); }); }
private async void ButtonSystemNetHttp_OnClick(object sender, RoutedEventArgs e) { var httpClientHandler = new System.Net.Http.HttpClientHandler(); CookieCollection cookieCollection = httpClientHandler.CookieContainer.GetCookies(_uri); var cookies = new Cookie[cookieCollection.Count]; cookieCollection.CopyTo(cookies, 0); using (var httpClient = new System.Net.Http.HttpClient()) { var requestMessage = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, _uri); Request.Text = new StringBuilder().AppendLine(requestMessage.ToString()) .AppendLine(requestMessage.Headers.ToString()) .AppendLine(string.Join("\n", cookies.ToList())).ToString(); System.Net.Http.HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage); Request1.Text = new StringBuilder().AppendLine(responseMessage.RequestMessage.ToString()) .AppendLine(string.Join(" - ", cookies.ToList())).ToString(); Response.Text = responseMessage.ToString(); } }
/// <inheritdoc /> public void CopyTo(Array array, int index) => _collection.CopyTo(array, index);