C#使用RestSharp实现post发送

最近做C#上传,不过在使用HttpWebRequest时遇到了奇怪的超时问题,一时无法解决。

于是找找其他办法,发现了RestSharp,快捷,好用,用法如下:

using RestSharp;

string url = "http://...";
string content = "..."; //要发送的内容
string contentType = "application/json"; //Content-Type

try
{
    var client = new RestClient(url);
    var request = new RestRequest(Method.POST);
    request.Timeout = 10000;
    //request.AddHeader("Cache-Control", "no-cache");          
    request.AddParameter(contentType, content, ParameterType.RequestBody);

    IRestResponse response = client.Execute(request);
    return response.Content; //返回的结果
}
catch (Exception ex)
{
     return "连接服务器出错:\r\n" + ex.Message;
}

支持文件:RestSharp.dll

猜你喜欢

转载自blog.csdn.net/zhouyingge1104/article/details/83378079