Esempio n. 1
0
        public static FormValues AsFormValues(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
        {
            var formValues = source as FormValues;

            if (formValues == null)
            {
                formValues = new FormValues();
                foreach (var u in source.GetType().GetProperties(bindingAttr).Where(p => p.GetValue(source, null) != null))
                {
                    formValues.Add(u.Name, u.GetValue(source, null).ToString());
                }
            }
            return(formValues);
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> Post(string url, FormValues formValues, FormValues defaults)
        {
            foreach (var value in defaults)
            {
                formValues[value.Key] = value.Value;
            }
            ResponseMsg = await SendRequest(HttpMethod.Post, url, formValues);

            if (ResponseMsg.StatusCode != HttpStatusCode.OK)
            {
                File.WriteAllText($"error{++_errorNumber}.html", Html);
                File.WriteAllText($"error{_errorNumber}.txt", string.Join(
                                      "\n", formValues.Select(a => $"'{a.Key}'='{a.Value}'")
                                      ));
            }
            return(await Task.FromResult(ResponseMsg));
        }
Esempio n. 3
0
        public static FormValues FormValues(this XDocument htmlDocument, int formIndex = 1)
        {
            var nodes = htmlDocument.Descendants("form").ElementAt(formIndex - 1).Descendants("input");
            var kv    = new FormValues();

            foreach (var node in nodes)
            {
                var name = node.Attribute("name")?.Value;
                if (name != null)
                {
                    if (kv.ContainsKey(name))
                    {
                        kv[name] = WebUtility.HtmlDecode(node.Attribute("value")?.Value ?? "");
                    }
                    else
                    {
                        kv.Add(name, WebUtility.HtmlDecode(node.Attribute("value")?.Value ?? ""));
                    }
                }
            }
            return(kv);
        }
Esempio n. 4
0
        private async Task <HttpResponseMessage> SendRequest(HttpMethod httpMethod, string url, FormValues formValues)
        {
            var request = new HttpRequestMessage(httpMethod, url);

            request.Headers.Add("Accept-Language", "en-US");
            request.Headers.Add("cookie", _cookies.Select(c => c.Key + "=" + c.Value.Value));
            if (formValues != null)
            {
                request.Content = new FormUrlEncodedContent(formValues);
            }

            if (url.StartsWith("http") && request.RequestUri.Host != "localhost")
            {
                using (var handler = new HttpClientHandler())
                    using (var client = new HttpClient(handler))
                    {
                        ResponseMsg = await client.GetAsync(request.RequestUri.ToString());
                    }
            }
            else
            {
                ResponseMsg = await _client.SendAsync(request);
            }
            if (!url.StartsWith("https://accounts.google.com/o/oauth2/auth"))
            {
                _cookies.Add(ResponseMsg);
            }
            if (ResponseMsg.Headers.Location != null)
            {
                return(await Get(ResponseMsg.Headers.Location.OriginalString));
            }
            Html = await ResponseMsg.Content.ReadAsStringAsync();

            try
            {
                HtmlDocument = Html.StartsWith("<!") ? XDocument.Parse(Html) : Html.StartsWith("{") ? new XDocument() : XDocument.Parse("<root>" + Html + "</root>");
            }
            catch (Exception e)
            {
                if (url.StartsWith("http://accounts") || url.StartsWith("/"))
                {
                    Debugger.Launch();
                    throw new Exception("Not a Xhtml Document", e);
                }
            }
            return(ResponseMsg);
        }