private void ClearParameters() { Body = null; Query.Clear(); Headers.Clear(); FormBody.Clear(); }
public IHttpService AddParameter(string type, string name, object value) { var adaptedValue = value is decimal?value.ToString().Replace(".", "").Replace(",", ".") : value.ToString(); switch (type) { case "Body": Body = value; break; case "FormBody": FormBody.Add(name, adaptedValue); break; case "Header": Headers.Add(name, adaptedValue); break; case "Query": Query.Add(name, adaptedValue); break; default: throw new ArgumentOutOfRangeException(nameof(type), type); } return(this); }
/// <summary> /// 设置表单数据 /// content-type=application/x-www-form-urlencoded /// </summary> /// <param name="parameters"></param> /// <returns></returns> public INoneBodyBuilder Form(IDictionary <string, object> parameters) { var body = new FormBody(); body.AddParameter(parameters); _requestBody = body; ContentType("application/x-www-form-urlencoded"); return(this); }
public void TestFormBody() { FormBody body = new FormBody(); Assert.Null(body.GetBody()); body.AddParameter(new { id = 1, name = "name" }); object p = null; body.AddParameter(p); body.AddParameter(new Dictionary <string, object> { { "title", "abc" } }); body.AddParameter(new Dictionary <string, object> { }); Assert.True(body.GetBody().Length > 0); }
private async Task <bool> Login(Proxy proxy, string username, string password) { using (WebSession session = new WebSession(proxy)) { RequestResponse rr = session.DispatchRequest(new GetRequest("https://www.pornhub.com/login")); if (!rr.Validate()) { Debug.WriteLine("Failed to load login page"); return(false); } Element tokenEl = rr.GetAsDoc().Select("[name=token]").First(); if (tokenEl is null) { Debug.WriteLine("Token not found"); return(false); } string token = tokenEl.Val; FormBody fb = new FormBody(); fb.Add("loginPage", 1); fb.Add("redirect", ""); fb.Add("token", token); fb.Add("taste_profile", ""); fb.Add("username", username); fb.Add("password", password); fb.Add("remember_me", "on"); rr = session.DispatchRequest(new PostRequest("https://www.pornhub.com/front/authenticate", fb)); if (!rr.Validate()) { if (rr.ResponseCode == 500) { Debug.WriteLine("Blocked connection"); return(false); } Debug.WriteLine("Failed to login"); return(false); } Debug.WriteLine(rr.GetResponseContent()); return(true); } }
public static async Task InitializeFormBodyDynamically(HttpClient http, Guid formId, Guid?contentId, FormBody formBody) { if (formBody == null) { throw new ArgumentNullException(nameof(formBody)); } Console.WriteLine("[Forms] Downloading form description"); var description = await http.GetJsonAsync <ConfiguratorFormDescription>($"/api/forms-runtime/forms/{formId}/description"); Console.WriteLine("[Forms] Downloading editing session initial values"); var initialValues = await http.PostJsonAsync <EditingSession>($"/api/forms-runtime/session?formId={formId}&contentId={contentId}", null); Console.WriteLine("[Forms] Setting form layout"); formBody.SetFormLayout(description); Console.WriteLine("[Forms] Initializing session"); formBody.StartNewSession(initialValues); }
/// <summary> /// 获取相应对象 /// </summary> /// <returns></returns> public HttpWebResponse GetResponse() { var timer = Stopwatch.StartNew(); string[] ms = new string[3]; if (Encoding == null) { Encoding = Encoding.UTF8; } ResponseCode = 0; Exception = null; Uri uri = GetFullUrl(); Trace.WriteLine(uri.ToString(), "HttpRequest.Url"); var www = WebRequest.CreateHttp(uri); if (uri.Scheme == Uri.UriSchemeHttps) { www.ProtocolVersion = HttpVersion.Version10; } www.CookieContainer = Cookie; www.Timeout = (int)Timeout.TotalMilliseconds; Headers.SetHeaders(www); www.Method = GetMethod(); www.KeepAlive = KeepAlive; try { _Abort = www.Abort; _RequestCreated = DateTime.Now; if (_FormBody != null) { FormBody.SetHeaders(www, Encoding); var formdata = FormBody.GetBytes(Encoding); www.ContentLength = formdata.Length; if (formdata.Length > 0) { using (var req = www.GetRequestStream()) { if (req.CanTimeout) { req.WriteTimeout = 3000; } req.Write(formdata, 0, formdata.Length); } } } else { www.ContentLength = 0; } ms[0] = "set data:" + timer.ElapsedMilliseconds; timer.Restart(); _RequestCreated = DateTime.Now; var res = (HttpWebResponse)www.GetResponse(); _Abort = null; ms[1] = "response:" + timer.ElapsedMilliseconds; timer.Restart(); www.CookieContainer = new CookieContainer(); www.CookieContainer.Add(res.Cookies); Headers.Clear(); Headers.Add(res.Headers); ResponseCode = res.StatusCode; return(res); } catch (WebException ex) { _Abort = null; Exception = ex; Trace.WriteLine(ex.Message, "HttpRequest.Error"); var res = ex.Response as HttpWebResponse; ms[1] = "error:" + timer.ElapsedMilliseconds; timer.Restart(); if (res != null) { www.CookieContainer = new CookieContainer(); www.CookieContainer.Add(res.Cookies); Headers.Clear(); Headers.Add(res.Headers); ResponseCode = res.StatusCode; return(res); } else { ResponseCode = (HttpStatusCode)ex.Status; return(null); } throw; } finally { ms[2] = "end:" + timer.ElapsedMilliseconds; Trace.WriteLine(ResponseCode, "HttpRequest.StatusCode"); Trace.WriteLine(string.Join("; ", ms), "HttpRequest.Timing"); } }
/// <summary> /// 获取相应对象 /// </summary> /// <returns></returns> public async Task <HttpResponseMessage> GetResponse() { var timer = Stopwatch.StartNew(); long pre = 0, setheader = 0, setbody = 0, res = 0, acceptcookie = 0, acceptheader = 0, error = 0, end = 0; if (Encoding == null) { Encoding = Encoding.UTF8; } if (PreRequest != null) { await PreRequest.ConfigureAwait(false); PreRequest = null; pre = timer.ElapsedMilliseconds; timer.Restart(); } ResponseCode = 0; Exception = null; Uri uri = GetFullUrl(); Trace.WriteLine(uri.ToString(), "HttpRequest.Url"); var cancel = new CancellationTokenSource(Timeout); try { _Abort = cancel.Cancel; var message = new HttpRequestMessage(GetHttpMethod(), uri); if (Cookie?.Count > 0) { message.Headers.Add("Cookie", Cookie.GetCookieHeader(uri)); } if (KeepAlive) { message.Headers.Connection.Add("keep-alive"); } if (_FormBody != null) { var formdata = FormBody.GetBytes(Encoding); message.Content = new ByteArrayContent(formdata); message.Content.Headers.Add("Content-Type", FormBody.ContentType); message.Content.Headers.ContentType.CharSet = Encoding.WebName; //message.Headers.TryAddWithoutValidation("Content-Type", "charset=" + Encoding.WebName); setbody = timer.ElapsedMilliseconds; timer.Restart(); } if (_Headers?.Count > 0) { foreach (var item in _Headers) { var arr = item.Value as IEnumerable <string>; if (arr != null) { if (!message.Headers.TryAddWithoutValidation(item.Key, arr) && message.Content != null) { message.Content.Headers.TryAddWithoutValidation(item.Key, arr); } } else { var str = item.Value as string ?? item.Value + ""; if (!message.Headers.TryAddWithoutValidation(item.Key, str) && message.Content != null) { message.Content.Headers.TryAddWithoutValidation(item.Key, str); } } } setheader = timer.ElapsedMilliseconds; timer.Restart(); } var response = await HttpClient.SendAsync(message, cancel.Token); _Abort = null; res = timer.ElapsedMilliseconds; timer.Restart(); if (AcceptCookie && response.Headers.Contains("Set-Cookie")) { var setcookies = response.Headers.GetValues("Set-Cookie"); foreach (var cookie in setcookies) { Cookie.SetCookies(uri, cookie); } acceptcookie = timer.ElapsedMilliseconds; timer.Restart(); } if (AcceptHeader) { Headers.Clear(); foreach (var head in response.Headers) { Headers.Add(head.Key, head.Value); } foreach (var head in response.Content.Headers) { Headers.Add(head.Key, head.Value); } acceptheader = timer.ElapsedMilliseconds; timer.Restart(); } ResponseCode = response.StatusCode; return(response); } catch (Exception ex) { error = timer.ElapsedMilliseconds; timer.Restart(); if (ex is TaskCanceledException && _Abort != null) { ex = new TimeoutException("请求已超时"); } Trace.WriteLine(ex.Message, "HttpRequest.Error"); Exception = ex; ResponseCode = 0; return(null); } finally { cancel?.Dispose(); _Abort = null; setbody = timer.ElapsedMilliseconds; timer.Stop(); Trace.WriteLine(ResponseCode, "HttpRequest.StatusCode"); Trace.WriteLine( $"pre={pre}; setheader={setheader}; setbody={setbody}; response={res}; acceptcookie={acceptcookie}; acceptheader={acceptheader}; error={error}; end={end}", "HttpRequest.Timing"); } }