public void DoWork() { _request.Get(_path); if (_request.StatusCode == HttpStatusCode.OK) { _enqueue( new SaveContent(_collector, _path, _enqueue, _request) .DoWork ); } else if (_request.StatusCode == HttpStatusCode.Redirect || _request.StatusCode == HttpStatusCode.Moved || _request.StatusCode == HttpStatusCode.SeeOther) { _enqueue( new SaveRedirect(_collector, _path, _enqueue, _request) .DoWork ); } else if (_request.StatusCode == HttpStatusCode.NotModified) { _enqueue( new SaveNotModified(_collector, _path, _enqueue, _request) .DoWork ); } else { _enqueue( new SaveHttpError(_collector, _path, _enqueue, _request) .DoWork ); } }
public SMSResult SentSMS(string phone, int code) { Dictionary <string, string> paramDict = new Dictionary <string, string>(); paramDict.Add("phone", phone); paramDict.Add("code", Convert.ToString(code)); return(HttpRequestUtil.Get <SMSResult>(Const.SMSUrl, paramDict)); }
public WxAuthToken GetAuthToken(WxContext wxContext, string code) { Dictionary <string, string> paramDict = new Dictionary <string, string>(); paramDict.Add("appid", wxContext.AuthInfo.AppId); paramDict.Add("secret", wxContext.AuthInfo.AppSercet); paramDict.Add("code", code); paramDict.Add("grant_type", "authorization_code"); return(HttpRequestUtil.Get <WxAuthToken>(Const.WxAuthTokenUrl, paramDict)); }
public void GetAuthCode(WxContext wxContext, string redirectUrl) { //?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect Dictionary <string, string> paramDict = new Dictionary <string, string>(); paramDict.Add("appid", wxContext.AuthInfo.AppId); paramDict.Add("redirect_uri", redirectUrl); paramDict.Add("response_type", "code"); paramDict.Add("scope", "snsapi_base#wechat_redirect"); HttpRequestUtil.Get <string>(Const.WxAuthCodeUrl, paramDict); }
/// <summary> /// 获取微信用户基本信息 /// </summary> /// <param name="wxContext"></param> /// <param name="openId"></param> /// <returns></returns> public WxUserInfo GetWxUserInfo(WxContext wxContext, string openId) { if (wxContext.Token == null || wxContext.Token.ExpiresTime <= DateTime.Now) { this.GetToken(wxContext); } Dictionary <string, string> paramDict = new Dictionary <string, string>(); paramDict.Add("access_token", wxContext.Token.Access_Token); paramDict.Add("openid", openId); paramDict.Add("lang", "zh_CN"); return(HttpRequestUtil.Get <WxUserInfo>(Const.WxUserInfo, paramDict)); }
public ICCIDCheckResult GetICCIDCheckResult(string iccId) { Dictionary <string, string> paramDict = new Dictionary <string, string>(); paramDict.Add("iccid", iccId); HttpClient httpClient = new HttpClient(); //httpClient.DefaultRequestHeaders.Host = "iccidchaxun.com"; httpClient.DefaultRequestHeaders.Referrer = new Uri("http://iccidchaxun.com/"); var result = HttpRequestUtil.Get <ICCIDCheckResult>(httpClient, Const.ICCIDCheckUrl, paramDict); return(result); }
public string Authorization(string code) { try { string url = $"https://api.weixin.qq.com/sns/jscode2session?appid={APPID}&secret={APPSECRET}&js_code={code}&grant_type=authorization_code"; string response = HttpRequestUtil.Get(url); return(response); } catch (Exception e) { throw; } }
/// <summary> /// 获取微信普通access_token /// </summary> /// <param name="wxContext"></param> /// <returns></returns> public WxToken GetToken(WxContext wxContext) { if (wxContext.Token.ExpiresTime > DateTime.Now) { return(wxContext.Token); } Dictionary <string, string> paramDict = new Dictionary <string, string>(); paramDict.Add("grant_type", "client_credential"); paramDict.Add("appid", wxContext.AuthInfo.AppId); paramDict.Add("secret", wxContext.AuthInfo.AppSercet); var token = HttpRequestUtil.Get <WxToken>(Const.WxTokenUrl, paramDict); wxContext.Token = token; return(token); }
/// <summary> /// 获取微信jssdk算签名用的ticket /// </summary> /// <param name="wxContext"></param> /// <returns></returns> public WxTicket GetTicket(WxContext wxContext) { if (wxContext.Ticket.ExpiresTime > DateTime.Now) { return(wxContext.Ticket); } Dictionary <string, string> paramDict = new Dictionary <string, string>(); paramDict.Add("access_token", GetToken(wxContext).Access_Token); paramDict.Add("type", "jsapi"); var ticket = HttpRequestUtil.Get <WxTicket>(Const.WxTicketUrl, paramDict); wxContext.Ticket = ticket; return(ticket); }
public async Task <IActionResult> Index() { try { var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/aspnet/AspNetCore.Docs/branches"); request.Headers.Add("Accept", "application/vnd.github.v3+json"); request.Headers.Add("User-Agent", "HttpClientFactory-Sample"); var client = _clientFactory.CreateClient(); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var strResult = await response.Content .ReadAsStringAsync(); } else { } string url = "www.baidu.com"; var result = await _httpRequestUtil.Get("https://api.github.com/repos/aspnet/AspNetCore.Docs/branches"); var posrResulit = await _httpRequestUtil.Post("http://www.baidu.com/api/getuserinfo", "userid=23456798"); } catch (Exception ex) { } return(View()); }
public void Benchmark( [Argument("page", "p", Description = "The http address of the web page to fetch.")] string url, [Argument(DefaultValue = 60, Description = "The duration of a single run of the test")] int duration, [Argument(DefaultValue = 1, Description = "The number of times to repeat the duration of the test")] int repeated, [Argument(DefaultValue = 0, Description = "The number of threads to run")] int threads, [Argument(DefaultValue = false, Description = "Cancel the test on the first http error.")] bool stopOnError) { Uri uri = new Uri(url, UriKind.Absolute); Uri baseUri = new Uri(uri, "/"); string uriPath = uri.PathAndQuery; if (threads <= 0) { threads = Environment.ProcessorCount; } repeated = Math.Max(1, repeated); duration = Math.Max(1, duration); int[] errors = { 0 }; int[] total = { 0 }; ManualResetEvent start = new ManualResetEvent(false); ManualResetEvent stop = new ManualResetEvent(false); Action <string> proc = x => { start.WaitOne(); while (!stop.WaitOne(0)) { HttpRequestUtil http = new HttpRequestUtil(baseUri); if (http.Get(x) != System.Net.HttpStatusCode.OK) { Interlocked.Increment(ref errors[0]); if (stopOnError) { throw new ApplicationException("Server returned " + http.StatusCode); } } else { Interlocked.Increment(ref total[0]); } } }; for (int run = 0; run <= repeated; run++) { errors[0] = total[0] = 0; start.Reset(); stop.Reset(); Stopwatch timer = new Stopwatch(); using (WorkQueue <string> worker = new WorkQueue <string>(proc, threads)) { worker.OnError += (o, e) => stop.Set(); for (int i = 0; i < threads; i++) { worker.Enqueue(uriPath); } Thread.Sleep(1000); start.Set(); timer.Start(); Thread.Sleep(1000 * (run == 0 ? 1 : duration)); stop.Set(); worker.Complete(true, 10000); } timer.Stop(); if (run > 0) { Console.WriteLine("Run {0,3}: {1,6} requests, {2,6} failures, in {3,6:n0}ms \t {4,6:n0} req/sec", run, total[0], errors[0], timer.ElapsedMilliseconds, total[0] / timer.Elapsed.TotalSeconds); } } }