public static string RetureResultes(string Url, string prament, Method method) { var client = new RestSharpClient(Url); var content = ""; client.ExecuteAsync <List <string> >(new RestRequest("?" + prament, method), result => { content = result.Content; }); return(content); }
/// <summary> /// RestSharp异步请求 /// </summary> /// <typeparam name="T">List<string></typeparam> /// <param name="Url">请求地址</param> /// <param name="prament">请求参数</param> /// <param name="methoh">请求方式(GET, POST, PUT, HEAD, OPTIONS, DELETE)</param> /// <param name="callback">回调函数</param> public static void ReturnResult <T>(string Url, Dictionary <string, string> prament, Method methoh, Action <IRestResponse <T> > callback) where T : new() { var client = new RestSharpClient(AppContext.AppConfig.serverUrl + Url); var Params = ""; if (prament.Count != 0) { Params = "?" + string.Join("&", prament.Select(x => x.Key + "=" + x.Value).ToArray()); } Log4net.LogHelper.Info("请求地址:" + AppContext.AppConfig.serverUrl + Url + Params); client.ExecuteAsync <T>(new RestRequest(Params, methoh), callback); }
/// <summary> /// 开始请求 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { try { //初始化状态 toolStripStatusState.Text = "请求中..."; lbResCode.Text = "请求中..."; lbResCode.ForeColor = Color.Black; Control c = JsonViewer.Controls.Find("txtJson", true)[0]; ((TextBox)c)?.Clear(); richResHeader.Clear(); richExcutReq.Clear(); var endPoint = txtEndPoint.Text.Trim(); var resource = txtResource.Text.Trim(); var reqbody = richReqBody.Text; var userName = txtUserName.Text.Trim(); var pwd = txtPassword.Text.Trim(); var authType = cbAuthType.Text; var mediaType = cbMediaType.Text; //保存请求数据 SaveRestInfo(); //认证 IAuthenticator iaAuthenticator; if (authType == "NTLM") { ICredentials ic = new NetworkCredential(userName, pwd); iaAuthenticator = new NtlmAuthenticator(ic); } else if (authType == "Basic") { iaAuthenticator = new HttpBasicAuthenticator(userName, pwd); } else { //TODO iaAuthenticator = new HttpBasicAuthenticator(userName, pwd); //iaAuthenticator = new SimpleAuthenticator(userName, pwd);(userName, pwd); } if (string.IsNullOrEmpty(resource)) { Uri uri = new Uri(endPoint); resource = uri.AbsolutePath; endPoint = uri.AbsoluteUri.Replace(resource, ""); } IRestSharp iRestSharp = new RestSharpClient(endPoint, iaAuthenticator); IRestRequest iRequest = new RestRequest(new Uri(endPoint + resource)); iRequest.Method = (Method)Enum.Parse(typeof(Method), cbMethod.Text); foreach (var line in richCurrReqHeader.Lines) { var val = line.Trim(); if (val.Contains(":")) { var firstIndex = val.IndexOf(":", StringComparison.Ordinal); var key = val.Substring(0, firstIndex); var value = val.Substring(firstIndex + 1, val.Length - firstIndex - 1); iRequest.AddHeader(key, value); } else { MessageBox.Show("填写的请求头参数不正确"); break; } } if (!string.IsNullOrEmpty(mediaType)) { iRequest.AddParameter(mediaType, reqbody, ParameterType.RequestBody); } //var iResponse = iRestSharp.Execute(iRequest); var asyncHandle = iRestSharp.ExecuteAsync(iRequest, response => { this.Invoke(new Action(() => { toolStripStatusState.Text = response.ResponseStatus.ToString(); })); var headerStr = ""; foreach (var parameter in response.Headers) { headerStr += parameter.Name + ":" + parameter.Value + "\n"; } richExcutReq.Invoke(new Action(() => { richExcutReq.Clear(); string reqContentType = "", reqBody = ""; foreach (var requestParameter in response.Request.Parameters) { if (requestParameter.Type == ParameterType.RequestBody) { reqContentType = requestParameter.Name; reqBody = requestParameter.Value.ToString(); } else { richExcutReq.AppendText(requestParameter.Name + ":" + requestParameter.Value + "\n"); } } richExcutReq.AppendText("\n*****************************" + reqContentType + "*******************************\n"); richExcutReq.AppendText(reqBody); })); this.Invoke(new Action(() => { richResHeader.Text = headerStr.Trim(':'); lbResCode.Text = Convert.ToInt32(response.StatusCode) + " " + response.StatusCode; lbResCode.ForeColor = response.StatusCode == HttpStatusCode.OK ? Color.Green : Color.Red; })); JsonViewer.Invoke(new Action(() => { JsonViewer.Json = response.Content; toolStripStatusState.Text = response.ResponseStatus.ToString(); })); }); // abort the request on demand //asyncHandle.Abort(); } catch (Exception ex) { MessageBox.Show(ex.Message, "请求失败,请检查配置信息"); } }