public async Task HttpClientJsonRequestTest() { var handler = new WebRequestHandler(); //handler.Proxy = new WebProxy("http://localhost:8888/"); //handler.Credentials = new NetworkCredential(uid, pwd); var client = new HttpClient(handler); var postSnippet = new CodeSnippet() { UserId = "Bogus", Code = "string.Format('Hello World, I will own you!');", Comment = "World domination imminent" }; // this isn't actually an API so the POST is ignored // but it always returns a JSON response string url = "http://codepaste.net/recent?format=json"; var response = await client.PostAsync(url, postSnippet, new JsonMediaTypeFormatter(), null); Assert.IsTrue(response.IsSuccessStatusCode); var snippets = await response.Content.ReadAsAsync<List<CodeSnippet>>(); Assert.IsTrue(snippets.Count > 0); foreach (var snippet in snippets) { if (string.IsNullOrEmpty(snippet.Code)) continue; Console.WriteLine(snippet.Code.Substring(0, Math.Min(snippet.Code.Length, 200))); Console.WriteLine("--"); } }
public async Task JsonRequestPostAsyncTest() { var postSnippet = new CodeSnippet() { UserId = "Bogus", Code = "string.Format('Hello World, I will own you!');", Comment = "World domination imminent" }; var settings = new HttpRequestSettings() { Url = "http://codepaste.net/recent?format=json", Content = postSnippet, HttpVerb = "POST" }; var snippets = await HttpUtils.JsonRequestAsync <List <CodeSnippet> >(settings); Assert.IsNotNull(snippets); Assert.IsTrue(settings.ResponseStatusCode == System.Net.HttpStatusCode.OK); Assert.IsTrue(snippets.Count > 0); Console.WriteLine(snippets.Count); Console.WriteLine(settings.CapturedRequestContent); Console.WriteLine(); Console.WriteLine(settings.CapturedResponseContent); foreach (var snippet in snippets) { if (string.IsNullOrEmpty(snippet.Code)) { continue; } Console.WriteLine(snippet.Code.Substring(0, Math.Min(snippet.Code.Length, 200))); Console.WriteLine("--"); } // This doesn't work for the async version - Response is never set by the base class Console.WriteLine("Status Code: " + settings.Response.StatusCode); foreach (var header in settings.Response.Headers) { Console.WriteLine(header + ": " + settings.Response.Headers[header.ToString()]); } }
public void JsonRequestPostTest() { var postSnippet = new CodeSnippet() { UserId = "Bogus", Code = "string.Format('die Bären sind süss und sauer.');", Comment = "World domination imminent" }; var settings = new HttpRequestSettings() { Url = "http://codepaste.net/recent?format=json", Content = postSnippet, HttpVerb = "POST" }; var snippets = HttpUtils.JsonRequest <List <CodeSnippet> >(settings); Assert.IsNotNull(snippets); Assert.IsTrue(settings.ResponseStatusCode == System.Net.HttpStatusCode.OK); Assert.IsTrue(snippets.Count > 0); Console.WriteLine(snippets.Count); Console.WriteLine(settings.CapturedRequestContent); Console.WriteLine(); Console.WriteLine(settings.CapturedResponseContent); foreach (var snippet in snippets) { if (string.IsNullOrEmpty(snippet.Code)) { continue; } Console.WriteLine(snippet.Code.Substring(0, Math.Min(snippet.Code.Length, 200))); Console.WriteLine("--"); } Console.WriteLine("Status Code: " + settings.Response.StatusCode); foreach (var header in settings.Response.Headers) { Console.WriteLine(header + ": " + settings.Response.Headers[header.ToString()]); } }
public async Task HttpClientJsonRequestTest() { var handler = new WebRequestHandler(); //handler.Proxy = new WebProxy("http://localhost:8888/"); //handler.Credentials = new NetworkCredential(uid, pwd); var client = new HttpClient(handler); var postSnippet = new CodeSnippet() { UserId = "Bogus", Code = "string.Format('Hello World, I will own you!');", Comment = "World domination imminent" }; // this isn't actually an API so the POST is ignored // but it always returns a JSON response string url = "http://codepaste.net/recent?format=json"; var response = await client.PostAsync(url, postSnippet, new JsonMediaTypeFormatter(), null); Assert.IsTrue(response.IsSuccessStatusCode); var snippets = await response.Content.ReadAsAsync <List <CodeSnippet> >(); Assert.IsTrue(snippets.Count > 0); foreach (var snippet in snippets) { if (string.IsNullOrEmpty(snippet.Code)) { continue; } Console.WriteLine(snippet.Code.Substring(0, Math.Min(snippet.Code.Length, 200))); Console.WriteLine("--"); } }
public async Task JsonRequestPostAsyncTest() { var postSnippet = new CodeSnippet() { UserId = "Bogus", Code = "string.Format('Hello World, I will own you!');", Comment = "World domination imminent" }; var settings = new HttpRequestSettings() { Url = "http://codepaste.net/recent?format=json", Content = postSnippet, HttpVerb = "POST" }; var snippets = await HttpUtils.JsonRequestAsync<List<CodeSnippet>>(settings); Assert.IsNotNull(snippets); Assert.IsTrue(settings.ResponseStatusCode == System.Net.HttpStatusCode.OK); Assert.IsTrue(snippets.Count > 0); Console.WriteLine(snippets.Count); Console.WriteLine(settings.CapturedRequestContent); Console.WriteLine(); Console.WriteLine(settings.CapturedResponseContent); foreach (var snippet in snippets) { if (string.IsNullOrEmpty(snippet.Code)) continue; Console.WriteLine(snippet.Code.Substring(0, Math.Min(snippet.Code.Length, 200))); Console.WriteLine("--"); } // This doesn't work for the async version - Response is never set by the base class Console.WriteLine("Status Code: " + settings.Response.StatusCode); foreach (var header in settings.Response.Headers) { Console.WriteLine(header + ": " + settings.Response.Headers[header.ToString()]); } }
public void JsonRequestPostTest() { var postSnippet = new CodeSnippet() { UserId = "Bogus", Code = "string.Format('die Bären sind süss und sauer.');", Comment = "World domination imminent" }; var settings = new HttpRequestSettings() { Url = "http://codepaste.net/recent?format=json", Content = postSnippet, HttpVerb = "POST" }; var snippets = HttpUtils.JsonRequest<List<CodeSnippet>>(settings); Assert.IsNotNull(snippets); Assert.IsTrue(settings.ResponseStatusCode == System.Net.HttpStatusCode.OK); Assert.IsTrue(snippets.Count > 0); Console.WriteLine(snippets.Count); Console.WriteLine(settings.CapturedRequestContent); Console.WriteLine(); Console.WriteLine(settings.CapturedResponseContent); foreach (var snippet in snippets) { if (string.IsNullOrEmpty(snippet.Code)) continue; Console.WriteLine(snippet.Code.Substring(0, Math.Min(snippet.Code.Length, 200))); Console.WriteLine("--"); } Console.WriteLine("Status Code: " + settings.Response.StatusCode); foreach (var header in settings.Response.Headers) { Console.WriteLine(header + ": " + settings.Response.Headers[header.ToString()]); } }