public void HttpParametersTest() { Console.WriteLine("Test 1"); object obj = null; string str = null; int? @int = null; var parameters = new HttpParameterCollection { { "test", obj }, { "test2", str }, { "test3", @int } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 2"); parameters = new HttpParameterCollection { { "n", 1 }, { "n", 2 }, { "n", 3 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 3"); parameters = new HttpParameterCollection { { "n[]", 1 }, { "n[]", 2 }, { "n[]", 3 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 4"); var constructorInfo = typeof(System.Web.HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First(); var f = (System.Web.HttpPostedFile)constructorInfo.Invoke(new object[] { "oauth.png", "image/png", null }); parameters = new HttpParameterCollection { { "file", "test.dat", "text/plain", new byte[] {1, 2, 3, 4, 5} }, { "file2", "test2.dat", new byte[] {1, 2, 3, 4, 5} }, { "file3", "test3.dat", new MemoryStream(Properties.Resources.oauth) }, { "file4", f }, { "x", 123 }, { "y", 456 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 5"); parameters = new HttpParameterCollection { { new byte[] {1, 2, 3, 4, 5} }, { "x", 123 }, { "y", 456 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 6"); parameters = new HttpParameterCollection { { f }, { "x", 123 }, { "y", 456 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 7"); parameters = new HttpParameterCollection { { Properties.Resources.oauth }, { "x", 123 }, { "x", 789 }, { "y", 456 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 8"); object obj2 = Properties.Resources.oauth; parameters = new HttpParameterCollection { { obj2 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 9"); var parameters2 = new NameValueCollection { { "a", "123" }, { "a", "245" }, { "b", "abc" } }; foreach (var p in (HttpParameterCollection)parameters2) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(((HttpParameterCollection)parameters2).ToStringParameters()); }
public void HttpParametersTest() { Console.WriteLine("Test 1"); object obj = null; string str = null; int? @int = null; var parameters = new HttpParameterCollection { { "test", obj }, { "test2", str }, { "test3", @int } }; foreach (var p in parameters) { Assert.AreEqual(HttpParameterType.Unformed, p.ParameterType); Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } string r = parameters.ToStringParameters(); Assert.AreEqual("test=&test2=&test3=", r); Console.WriteLine(r); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 2"); parameters = new HttpParameterCollection { { "n", 1 }, { "n", 2 }, { "n", 3 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } r = parameters.ToStringParameters(); Assert.AreEqual("n=1&n=2&n=3", r); Console.WriteLine(r); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 3"); parameters = new HttpParameterCollection { { "n[]", 1 }, { "n[]", 2 }, { "n[]", 3 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } r = parameters.ToStringParameters(); Assert.AreEqual("n%5b%5d=1&n%5b%5d=2&n%5b%5d=3", r); Console.WriteLine(r); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 4"); var constructorInfo = typeof(System.Web.HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First(); var f = (System.Web.HttpPostedFile)constructorInfo.Invoke(new object[] { "oauth.png", "image/png", null }); parameters = new HttpParameterCollection { { "file", "test.dat", "text/plain", new byte[] { 1, 2, 3, 4, 5 } }, { "file2", "test2.dat", new byte[] { 1, 2, 3, 4, 5 } }, { "file3", "test3.dat", new MemoryStream(Properties.Resources.oauth) }, { "file4", f }, { "x", 123 }, { "y", 456 } }; for (int i = 0; i < parameters.Count; i++) { var p = parameters[i]; if (i < 3) { Assert.AreEqual(HttpParameterType.File, p.ParameterType); } Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } r = parameters.ToStringParameters(); Assert.AreEqual("x=123&y=456", r); Console.WriteLine(r); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 5"); parameters = new HttpParameterCollection { { new byte[] { 1, 2, 3, 4, 5 } }, { "x", 123 }, { "y", 456 } }; for (int i = 0; i < parameters.Count; i++) { var p = parameters[i]; if (i == 0) { Assert.AreEqual(HttpParameterType.RequestBody, p.ParameterType); } Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } r = parameters.ToStringParameters(); Assert.AreEqual("x=123&y=456", r); Console.WriteLine(r); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 6"); parameters = new HttpParameterCollection { { f }, { "x", 123 }, { "y", 456 } }; for (int i = 0; i < parameters.Count; i++) { var p = parameters[i]; if (i == 0) { Assert.AreEqual(HttpParameterType.RequestBody, p.ParameterType); } Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } r = parameters.ToStringParameters(); Assert.AreEqual("x=123&y=456", r); Console.WriteLine(r); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 7"); parameters = new HttpParameterCollection { { Properties.Resources.oauth }, { "x", 123 }, { "x", 789 }, { "y", 456 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } r = parameters.ToStringParameters(); Assert.AreEqual("x=123&x=789&y=456", r); Console.WriteLine(r); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 8"); object obj2 = Properties.Resources.oauth; parameters = new HttpParameterCollection { { obj2 } }; for (int i = 0; i < parameters.Count; i++) { var p = parameters[i]; if (i == 0) { Assert.AreEqual(HttpParameterType.RequestBody, p.ParameterType); } Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 9"); var parameters2 = new NameValueCollection { { "a", "123" }, { "a", "245" }, { "b", "abc" } }; foreach (var p in (HttpParameterCollection)parameters2) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } r = ((HttpParameterCollection)parameters2).ToStringParameters(); Assert.AreEqual("a=123%2c245&b=abc", r); Console.WriteLine(r); }
public void HttpParametersTest() { Console.WriteLine("Test 1"); object obj = null; string str = null; int? @int = null; var parameters = new HttpParameterCollection { { "test", obj }, { "test2", str }, { "test3", @int } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 2"); parameters = new HttpParameterCollection { { "n", 1 }, { "n", 2 }, { "n", 3 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 3"); parameters = new HttpParameterCollection { { "n[]", 1 }, { "n[]", 2 }, { "n[]", 3 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 4"); var constructorInfo = typeof(System.Web.HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First(); var f = (System.Web.HttpPostedFile)constructorInfo.Invoke(new object[] { "oauth.png", "image/png", null }); parameters = new HttpParameterCollection { { "file", "test.dat", "text/plain", new byte[] { 1, 2, 3, 4, 5 } }, { "file2", "test2.dat", new byte[] { 1, 2, 3, 4, 5 } }, { "file3", "test3.dat", new MemoryStream(Properties.Resources.oauth) }, { "file4", f }, { "x", 123 }, { "y", 456 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 5"); parameters = new HttpParameterCollection { { new byte[] { 1, 2, 3, 4, 5 } }, { "x", 123 }, { "y", 456 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 6"); parameters = new HttpParameterCollection { { f }, { "x", 123 }, { "y", 456 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 7"); parameters = new HttpParameterCollection { { Properties.Resources.oauth }, { "x", 123 }, { "x", 789 }, { "y", 456 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 8"); object obj2 = Properties.Resources.oauth; parameters = new HttpParameterCollection { { obj2 } }; foreach (var p in parameters) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(parameters.ToStringParameters()); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 9"); var parameters2 = new NameValueCollection { { "a", "123" }, { "a", "245" }, { "b", "abc" } }; foreach (var p in (HttpParameterCollection)parameters2) { Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } Console.WriteLine(((HttpParameterCollection)parameters2).ToStringParameters()); }
public void HttpParametersWriteTest() { Console.WriteLine("Test 1"); object obj = null; string str = null; int? @int = null; var parameters = new HttpParameterCollection { { "test", obj }, { "test2", str }, { "test3", @int } }; foreach (var p in parameters) { Assert.AreEqual(HttpParameterType.Unformed, p.ParameterType); Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } var req = (HttpWebRequest)WebRequest.Create("http://api.foxtools.ru/v2/hash"); req.Method = "POST"; parameters.WriteToRequestStream(req); string r = parameters.ToStringParameters(); Assert.AreEqual("test=&test2=&test3=", r); Console.WriteLine(r); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 2"); parameters = new HttpParameterCollection { new HttpUrlParameter("test", "123"), new HttpFormParameter("test2", null), new HttpFormParameter("text", "test") }; for (int i = 0; i < parameters.Count; i++) { var p = parameters[i]; if (i == 0) { Assert.AreEqual(HttpParameterType.Url, p.ParameterType); } else { Assert.AreEqual(HttpParameterType.Form, p.ParameterType); } Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } req = (HttpWebRequest)WebRequest.Create("http://api.foxtools.ru/v2/hash"); req.Method = "POST"; parameters.WriteToRequestStream(req); r = parameters.ToStringParameters(HttpParameterType.Url); Assert.AreEqual("test=123", r); Console.WriteLine("-------------------------------------"); Console.WriteLine("Test 3"); parameters = new HttpParameterCollection { new { a = 1, b = 2, c = "abc" } }; for (int i = 0; i < parameters.Count; i++) { var p = parameters[i]; Assert.AreEqual(HttpParameterType.RequestBody, p.ParameterType); Console.WriteLine("{0} is {1}", p.Name, p.GetType().Name); } req = (HttpWebRequest)WebRequest.Create("http://api.foxtools.ru/v2/hash"); req.Method = "POST"; req.ContentType = "application/json"; parameters.WriteToRequestStream(req); req.GetResponse(); req = (HttpWebRequest)WebRequest.Create("http://api.foxtools.ru/v2/hash"); req.Method = "POST"; parameters.WriteToRequestStream(req); req.GetResponse(); Console.WriteLine("-------------------------------------"); }