/// <summary> /// RestSharp同步请求 /// </summary> /// <param name="Url">接口基地址</param> /// <param name="prament">请求参数</param> /// <param name="method">请求方式</param> /// <returns></returns> public static string ReturnResult(string Url, string prament, Method method) { var client = new RestSharpClient(Url); var result = client.Execute(new RestRequest("?" + prament, method)); return(result.Content); }
/// <summary> /// RestSharp同步泛型请求 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="Url">地址</param> /// <param name="prament">参数</param> /// <param name="methoh">方式</param> /// <returns></returns> public static List <string> ReturnResults <T>(string Url, string prament, Method methoh) where T : new() { var client = new RestSharpClient(Url); var result = client.Execute <List <string> >(new RestRequest("?" + prament, methoh)); return(result); }
public void TestMethod1() { string host = "http://dongqiudi.com"; RestSharpClient client = new RestSharpClient(host); var response = client.Execute("archives/1", RestSharp.Method.GET, request => request.AddQueryParameter("page", "2")); Console.WriteLine(response.Content); }
public CallWindowView(User friend, WebSocketClient webSocketClient, RestSharpClient restClient, NotificationService notificationService, bool staarted) { _viewModel = new CallWindowViewModel(friend, webSocketClient, restClient, notificationService, staarted); DataContext = _viewModel; InitializeComponent(); this.AttachDevTools(); }
public CallRequestView(User caller, RestSharpClient restClient) { _caller = caller; _restClient = restClient; DataContext = caller; InitializeComponent(); this.AttachDevTools(); }
private SampleDTO GetPostedData() { RestSharpClient client = new RestSharpClient(); dynamic requestBody = new { title = "foo", body = "bar", userId = 1 }; return(client.Post <SampleDTO>("/", requestBody)); }
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); }
public TestClient(ILogging logging, IRestClient client, TestClientConfig config) : base(logging, client, config.BaseUrl) { _client = client; _config = config; _logging = logging; if (string.IsNullOrEmpty(config.BaseUrl)) { throw new NullReferenceException("PromoServicesHostname AppSettings does not exist."); } }
/// <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); }
public void GetRestaurantsByOutcode_Should_Returns_Data_For_The_Outcode_SE19() { // Given a valid outcode var outcode = "SE19"; // When we call the service var client = new RestSharpClient(); var restaurantService = new RestaurantService(client); var results = restaurantService.GetRestaurantsByOutcode(outcode); // Then the restaurant list is not null and not empty Assert.IsNotNull(results); Assert.Greater(results.Count, 0); }
public static string GetGeographyQuestions() { var req = new JsonRestRequest("api/questions", Method.GET); return(RestSharpClient.Execute(req)); }
public static async Task <T> GetGeographyQuestionsAsync <T>() where T : new() { var req = new JsonRestRequest("api/questions", Method.GET); return(await RestSharpClient.ExecuteAsyn <T>(req)); }
public static T GetGeographyQuestions <T>() where T : new() { var req = new JsonRestRequest("api/questions", Method.GET); return(RestSharpClient.Execute <T>(req)); }
/// <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, "请求失败,请检查配置信息"); } }