private T Get <T>(string serverUrl, string requestUrl, IDictionary <string, object> data = null) { T result = default(T); var restClient = new RestSharp.RestClient(serverUrl); var request = new RestSharp.RestRequest(requestUrl, RestSharp.Method.POST); if (data != null) { foreach (var key in data.Keys) { request.AddParameter(key, data[key]); } } // 追加登录Cookie var cookie = FormsAuthentication.GetAuthCookie(User.Identity.Name, true); request.AddCookie(FormsAuthentication.FormsCookieName, cookie.Value); var response = restClient.Execute(request); var json = response.Content; if (json.IndexOf("\"total\":0") < 0) { var dtStartIndex = json.IndexOf("[{"); var dtEndIndex = json.LastIndexOf("}]"); var dtJson = json.Substring(dtStartIndex, dtEndIndex - dtStartIndex + 2); result = JsonConvert.DeserializeObject <T>(dtJson); } return(result); }
private string Post(string apiBaseUri, string requestUrl, object data = null) { var restClient = new RestSharp.RestClient(apiBaseUri); var request = new RestSharp.RestRequest(requestUrl, RestSharp.Method.POST); request.RequestFormat = RestSharp.DataFormat.Json; //if (data != null) //{ // foreach (var key in data.Keys) // { // request.AddParameter(key, data[key]); // } //} request.AddBody(data); // 追加登录Cookie var cookie = FormsAuthentication.GetAuthCookie(User.Identity.Name, true); request.AddCookie(FormsAuthentication.FormsCookieName, cookie.Value); request.Timeout = 36000; var response = restClient.Execute(request); return(response.Content); }
private RestSharp.IRestRequest BuildRestSharpRequest(RestRequest restRequest) { var restSharpRequest = new RestSharp.RestRequest(restRequest.Resource); restSharpRequest.Method = (RestSharp.Method)Enum.Parse(typeof(RestSharp.Method), restRequest.Method.ToString()); restSharpRequest.RequestFormat = (RestSharp.DataFormat)Enum.Parse(typeof(RestSharp.DataFormat), restRequest.RequestFormat.ToString()); restSharpRequest.DateFormat = restRequest.DateFormat; //added by philip to force timeout within 15 seconds for connect restSharpRequest.Timeout = 15000; //TODO: solve this mapping //restSharpRequest.Credentials = restRequest.Credentials; if (restRequest.Body != null) { restSharpRequest.AddBody(restRequest.Body); } foreach (var item in restRequest.Cookies) { restSharpRequest.AddCookie(item.Key, item.Value); } foreach (var item in restRequest.Files) { if (!string.IsNullOrEmpty(item.Path)) { restSharpRequest.AddFile(item.Name, item.Path); } else { if (String.IsNullOrEmpty(item.ContentType)) { restSharpRequest.AddFile(item.Name, item.Bytes, item.FileName); } else { restSharpRequest.AddFile(item.Name, item.Bytes, item.FileName, item.ContentType); } } } foreach (var item in restRequest.Headers) { restSharpRequest.AddHeader(item.Key, item.Value); } foreach (var item in restRequest.UrlSegments) { restSharpRequest.AddUrlSegment(item.Key, item.Value); } foreach (var item in restRequest.Objects) { restSharpRequest.AddObject(item.Key, item.Value); } foreach (var item in restRequest.Parameters) { RestSharp.ParameterType t = (RestSharp.ParameterType)Enum.Parse(typeof(RestSharp.ParameterType), item.Type.ToString()); restSharpRequest.AddParameter( item.Name, item.Value, t ); } return(restSharpRequest); }