private HttpStatusCode Options(string url) { var req = HttpWebRequestBuilder.Build(new Uri(Urls.BASE + url)) .Setting(x => x.UserAgent, Defaults.USERAGENT) .Setting(x => x.Referer, Defaults.REFERER) .Setting(x => x.CookieContainer, Cookies) .Setting(x => x.Method, HttpMethods.OPTIONS) .SetHeader("Access-Control-Request-Method", "POST") .SetHeader("Access-Control-Request-Headers", "accept, csrf, content-type") .SetHeader("Origin", Defaults.ORIGIN) .Create(); var resp = req.GetResponse() as HttpWebResponse; return(resp.StatusCode); }
private T SendPayload <T>(string url, string method, T payload) { try { var sb = new StringBuilder(); // this finds the csrf cookie. if it exists, it will be added as a header var csrf = FindCookie("csrf"); foreach (Cookie f in Cookies.GetCookies(new Uri(Defaults.ORIGIN))) { Console.WriteLine("{0}:{1}", f.Name, f.Value); } var req = HttpWebRequestBuilder.Build(new Uri(Urls.BASE + url)) .Setting(x => x.UserAgent, Defaults.USERAGENT) .Setting(x => x.Referer, Defaults.REFERER) .Setting(x => x.CookieContainer, Cookies) .Setting(x => x.Accept, "application/json, text/javascript, */*; q=0.01") .Setting(x => x.ContentType, "application/json") .Setting(x => x.Method, method.Require()) .SetHeader("Origin", Defaults.ORIGIN) .SetHeader("csrf", csrf) .Create(); var serializer = JsonSerializationBuilder.Build().Create(); using (var serializedDataStream = new MemoryStream()) { using (var streamWriter = new StreamWriter(serializedDataStream)) { serializer.Serialize(streamWriter, payload); streamWriter.Flush(); // move to start serializedDataStream.Position = 0; using (var rs = req.GetRequestStream()) { serializedDataStream.CopyTo(rs); } var resp = req.GetResponse() as HttpWebResponse; if (resp.StatusCode != HttpStatusCode.OK) { throw new WebException(resp.StatusDescription, (WebExceptionStatus)resp.StatusCode); } } } return(payload); } catch (WebException wex) { Console.Error.WriteLine(wex.Message); } return(default(T)); }
private Stream GetDataStream(string url) { try { var sb = new StringBuilder(); var req = HttpWebRequestBuilder.Build(new Uri(Urls.BASE + url)) .Setting(x => x.UserAgent, Defaults.USERAGENT) .Setting(x => x.Referer, Defaults.REFERER) .Setting(x => x.CookieContainer, Cookies) .Setting(x => x.Method, HttpMethods.GET) .SetHeader("Origin", Defaults.ORIGIN) .Create(); var response = req.GetResponse() as HttpWebResponse; if (response.StatusCode == HttpStatusCode.OK) { return(response.GetResponseStream()); } } catch (WebException wex) { Console.Error.WriteLine(wex.Message); } return(null); }
/// <summary> /// Login to the Echo service. /// </summary> /// <returns></returns> /// <exception cref="System.Net.WebException"></exception> public bool Login() { // this needs to check if we need to log in, or if we are already logged in. var document = new HtmlDocument { OptionFixNestedTags = true }; document.LoadHtml(Get("")); var form = document.GetElementbyId(Defaults.LOGIN_FORM_ID).Require(); var action = form.Attributes.FirstOrDefault(x => x.Name == "action"); if (action == null || string.IsNullOrWhiteSpace(action.Value)) { return(false); } var urlAction = action.Value; var hiddens = form.SelectNodes("//input[@type=\"hidden\"]"); var kvps = new List <KeyValuePair <string, string> >(); kvps.Add(new KeyValuePair <string, string>("email", Username)); kvps.Add(new KeyValuePair <string, string>("password", Password)); kvps.Add(new KeyValuePair <string, string>("create", "0")); foreach (var item in hiddens) { kvps.Add(new KeyValuePair <string, string>(item.Attributes["name"].Value, item.Attributes["value"].Value)); } var postData = new StringBuilder(); foreach (var item in kvps) { postData.AppendFormat("{0}={1}&", item.Key, item.Value); } if (postData.Length > 0) { postData = postData.Remove(postData.Length - 1, 1); } var dataString = postData.ToString(); var dataBytes = Encoding.UTF8.GetBytes(dataString); var req = HttpWebRequestBuilder.Build(new Uri(urlAction)) .Setting(x => x.UserAgent, Defaults.USERAGENT) .Setting(x => x.Referer, Urls.BASE) .Setting(x => x.CookieContainer, Cookies) .Setting(x => x.Method, HttpMethods.POST) .Setting(x => x.ContentType, "application/x-www-form-urlencoded") .Setting(x => x.ContentLength, dataBytes.Length) .SetHeader("Origin", Defaults.ORIGIN) .Create(); using (var rstream = req.GetRequestStream()) { rstream.Write(dataBytes, 0, dataBytes.Length); } var resp = req.GetResponse() as HttpWebResponse; var statusCode = resp.StatusCode; if (statusCode == HttpStatusCode.OK) { return(true); } // resetting the container starts the session over. Cookies = new CookieContainer(); throw new WebException(resp.StatusDescription, (WebExceptionStatus)statusCode); }