public void TestCookieParsing() { OrderedDictionary headers = new OrderedDictionary(); headers.Add("method", "GET"); headers.Add("authority", "www.facebook.com"); headers.Add("scheme", "https"); headers.Add("path", "/"); headers.Add("pragma", "no-cache"); headers.Add("cache-control", "no-cache"); headers.Add("dnt", "1"); headers.Add("upgrade-insecure-requests", "1"); headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"); headers.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"); headers.Add("sec-fetch-site", "none"); headers.Add("sec-fetch-mode", "navigate"); headers.Add("sec-fetch-user", "?1"); headers.Add("sec-fetch-dest", "document"); headers.Add("sec-ch-ua", "\" Not; A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\""); headers.Add("sec-ch-ua-mobile", "?0"); headers.Add("accept-encoding", "gzip, deflate"); headers.Add("accept-language", "en-US,en;q=0.9"); RetObject r = Execute.HttpRequest.Send( "https://www.facebook.com/", HttpMethod.Get, headers ); Assert.IsTrue(r.CookieCollection.Count > 0); }
public void MultipartFormData() { Utils jsonParser = new Utils(); jsonParser.Ctor(); string readmefile = Directory.GetCurrentDirectory() + "\\..\\..\\..\\Execute.HttpRequest\\readme.txt"; string control_compare = File.ReadAllText(readmefile, Encoding.UTF8); RetObject r1 = Execute.HttpRequest.Send( "https://httpbin.org/post", HttpMethod.Post, null, null, "multipart/form-data", null, readmefile ); JsonDocument json1 = jsonParser.ConvertFromJson(r1.ResponseText); JsonElement returnFile1 = json1.RootElement.GetProperty("files"); string exp_compare1 = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(returnFile1.GetProperty("file").GetString())); RetObject r2 = Execute.HttpRequest.Send( "https://httpbin.org/post", HttpMethod.Post, null, null, "multipart/form-data", "this=is&my=test&form=data", readmefile ); JsonDocument json2 = jsonParser.ConvertFromJson(r2.ResponseText); JsonElement returnFile2 = json2.RootElement.GetProperty("files"); string exp_compare2 = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(returnFile2.GetProperty("file").GetString())); Assert.IsTrue(control_compare.Equals(exp_compare1)); Assert.IsTrue(control_compare.Equals(exp_compare2)); }
public void HTML_DOM_Test(string method) { RetObject r = Execute.HttpRequest.Send( title_check_uri, Utils.methods[method] ); Assert.IsTrue(r.HtmlDocument.title.ToString().Equals(title_match)); }
private static async Task <string> StartDownload(string Url, string filePath, bool silent = false) { ServicePointManager.DefaultConnectionLimit = 10; RetObject ret = Execute.HttpRequest.Send( Url, HttpMethod.Head ); length = long.Parse(ret.HttpResponseMessage.Content.Headers.ContentLength.ToString()); lengthMB = Math.Round(((Double)length / 1048576), 2); numberOfChunks = Convert.ToInt32(Math.Floor(Convert.ToDecimal((length / chunk)))) + 1; if (File.Exists(filePath)) { File.Delete(filePath); } start = DateTime.Now; FileStream fs = File.Open(filePath, FileMode.OpenOrCreate); for (Int32 i = 0; i < numberOfChunks; i++) { List <Int32> be = new List <Int32>(); be.Add((i * chunk)); be.Add((i * chunk + chunk - 1)); await Task.Factory.StartNew((b) => { long begin = ((List <Int32>)b)[0]; long end = ((List <Int32>)b)[1]; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url); req.ServicePoint.ConnectionLimit = 10; req.AllowReadStreamBuffering = false; req.AllowWriteStreamBuffering = false; req.AddRange(begin, end); using (HttpWebResponse res = (HttpWebResponse)req.GetResponse() as HttpWebResponse) { using (Stream stream = res.GetResponseStream()) { stream.CopyTo(fs); req.Abort(); res.Close(); res.Dispose(); } } }, be, TaskCreationOptions.LongRunning); if (!silent) { WriteProgress(be[0], be[1]); } } fs.Close(); fs.Dispose(); String[] sp = new string[ts.Length]; String[] bs = new string[ts.Length]; return("done"); }
public void TestSendHttp() { Execute.Utils utils = new Execute.Utils(); utils.Ctor(); Task <RetObject> t = utils.SendHttp(project_uri); Assert.IsInstanceOfType(t, typeof(Task <RetObject>)); RetObject r = t.Result; Assert.IsTrue(r.HttpResponseMessage.StatusCode.Equals(HttpStatusCode.OK)); }
public void TestTrace() { // FYI Although you can technically send an HTTP TRACE request with this library, I don't consider it to be fully implemented because: // 1) HTTP TRACE returns '405 method not allowed' on most modern webpages, // 2) I'm honestly a little confused about what a response from a successful TRACE request, to a webserver that allows it, actually looks like RetObject r = Execute.HttpRequest.Send( project_uri, HttpMethod.Trace ); Assert.IsTrue(r.HttpResponseMessage.StatusCode.Equals(HttpStatusCode.MethodNotAllowed)); }
public void HeadAndOptions(string method) { if (method.Equals("head")) { RetObject r = Execute.HttpRequest.Send( dluri, Utils.methods[method] ); Int32 returnedLength = Int32.Parse(r.HttpResponseMessage.Content.Headers.ContentLength.ToString()); Assert.IsTrue(returnedLength.Equals(UnitTest1.content_length)); } else { List <string> tw_return_allow_headers = new List <string>(); OrderedDictionary headers = new OrderedDictionary(); headers.Add("method", "OPTIONS"); headers.Add("authority", "api.twitter.com"); headers.Add("scheme", "https"); headers.Add("path", "/1.1/guest/activate.json"); headers.Add("pragma", "no-cache"); headers.Add("cache-control", "no-cache"); headers.Add("accept", "*/*"); headers.Add("access-control-request-method", "POST"); headers.Add("access-control-request-headers", "authorization,x-csrf-token,x-twitter-active-user,x-twitter-client-language"); headers.Add("origin", "https://twitter.com"); headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"); headers.Add("sec-fetch-mode", "cors"); headers.Add("sec-fetch-site", "same-site"); headers.Add("sec-fetch-dest", "empty"); headers.Add("referer", "https://twitter.com/"); headers.Add("accept-encoding", "gzip, deflate"); headers.Add("accept-language", "en-US,en;q=0.9"); RetObject r = Execute.HttpRequest.Send( tw_uri, HttpMethod.Options, headers ); Assert.IsTrue(r.HttpResponseMessage.Headers.Contains("access-control-allow-headers")); } }
public void TestCookieNullReturn() { CookieCollection collection = new CookieCollection(); Cookie cookie = new Cookie() { Name = "fakecookie", Value = "fakevalue", Domain = ".nstevens1040.github.io", Path = "/", Expires = DateTime.Now.AddYears(1), Secure = true, HttpOnly = false }; collection.Add(cookie); RetObject r = Execute.HttpRequest.Send( "https://nstevens1040.github.io/Execute.HttpRequest/", HttpMethod.Get, null, collection ); Assert.IsTrue(r.CookieCollection.Count == 1); }
public void HeadersAndCookies(string method) { string payload = "guid1=" + Guid.NewGuid().ToString() + "&guid2=" + Guid.NewGuid().ToString(); RetObject r = null; List <string> include_payload = new List <string>(); include_payload.Add("post"); include_payload.Add("put"); Utils jsonParser = new Utils(); jsonParser.Ctor(); string guid = Guid.NewGuid().ToString(); string cookie_value = Guid.NewGuid().ToString(); CookieCollection cc = new CookieCollection() { new Cookie() { Name = "test_cookie", Value = cookie_value, Path = "/" + method, Domain = "postman-echo.com", Secure = true, Expires = DateTime.Parse(@"1970-01-01").AddSeconds(1654093426), HttpOnly = false } }; OrderedDictionary headers = new OrderedDictionary(); headers.Add("x-test-header", guid); bool preq = false; if (include_payload.Contains(method)) { preq = true; r = Execute.HttpRequest.Send( "https://postman-echo.com/" + method, Utils.methods[method], headers, cc, "application/x-www-form-urlencoded", payload ); } else { r = Execute.HttpRequest.Send( "https://postman-echo.com/" + method, Utils.methods[method], headers, cc ); } JsonDocument json = jsonParser.ConvertFromJson(r.ResponseText); JsonElement returnHeaders = json.RootElement.GetProperty("headers"); string returnGuid = returnHeaders.GetProperty("x-test-header").GetString(); string cookie_comparison = "test_cookie=" + cookie_value; string returnCookie = returnHeaders.GetProperty("cookie").GetString(); Assert.IsTrue(new Regex(guid).Match(returnGuid).Success); Assert.IsTrue(new Regex(cookie_comparison).Match(returnCookie).Success); if (preq) { JsonElement f = json.RootElement.GetProperty("form"); string payload_echo = "guid1=" + f.GetProperty("guid1").GetString() + "&guid2=" + f.GetProperty("guid2").GetString(); Assert.IsTrue(payload_echo.Equals(payload)); } }
public void TestHttpResponseHeaders() { RetObject r = Execute.HttpRequest.Send("https://nstevens1040.github.io/Execute.HttpRequest/"); Assert.IsTrue(r.HttpResponseHeaders.Count > 0); }