public string Post(string url, string content)
{
string contentType = "application/json"; //Content-Type
try
{
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.Timeout = 10000;
request.AddParameter(contentType, content, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response.Content; //返回的结果
}
catch (Exception ex)
{
return "连接服务器出错:\r\n" + ex.Message;
}
}
public classRestClientManager
{public JObject Post(string baseUrl, stringurl, JObject sendData)
{return Post(baseUrl, url, sendData, string.Empty);
}public JObject Post(string baseUrl, string url, JObject sendData, stringtoken)
{
RestClient client= newRestClient(baseUrl);var request = newRestRequest(url, Method.POST);var contenType = "application/json";
request.AddHeader("Accept", contenType);if (!string.IsNullOrWhiteSpace(token))
{
request.AddHeader("token", token);
}if (sendData == null)
{
sendData= newJObject();
}
request.AddParameter(contenType, sendData, ParameterType.RequestBody);var response =client.Execute(request);if (string.IsNullOrWhiteSpace(response.Content))
{return null;
}returnJObject.Parse(response.Content);
}public JObject Get(string baseUrl, stringurl, JObject sendData)
{return Get(baseUrl, url, sendData, string.Empty);
}public JObject Get(string baseUrl, string url, JObject sendData, stringtoken)
{string parames = string.Empty;if (sendData != null)
{
StringBuilder datas= newStringBuilder();foreach (var item insendData)
{
datas.AppendFormat("{0}={1}", item.Key, item.Value);
}
parames= string.Format("?{0}", string.Join("&", datas));
}var client = new RestClient(string.Format("{0}{1}{2}", baseUrl, url, parames));var request = newRestRequest(Method.GET);var response =client.Execute(request);returnJObject.Parse(response.Content);
}
}