【问题标题】:Do i need to Dispose or Delete RestSharp Request before executing another Request在执行另一个请求之前,我是否需要处理或删除 RestSharp 请求
【发布时间】:2013-08-29 08:26:09
【问题描述】:

使用 RestSharp 我想使用来自 Windows 窗体应用程序的 Web 服务,使用 C# 来制作桌面应用程序。

我收到了第一个请求的响应, 但是当我发送另一个请求时,我得到一个超时错误。 这是代码

client = new RestClient();

client.BaseUrl = "webservicelink";

client.Authenticator = new DigestAuthenticator("usrnm", "pswd");

var request = new RestRequest();

request.AddParameter("key",JVP8xGk4hsX2cZd0L3NQwYbI0mf4exPiSoAhVYnz");

IRestResponse response = client.Execute(request);

【问题讨论】:

    标签: c# web-services rest desktop-application restsharp


    【解决方案1】:

    我认为您应该检查响应是否有效,并为下一个请求保存 cookie。 这样的事情可能会起作用:

    if (response.StatusCode != HttpStatusCode.OK && response.StatusCode !=   HttpStatusCode.Created)
    {
         throw new Exception(response.Content);
    }
    RestResponseCookie cookie = response.Cookies[0];
    
    var anotherRequest = new RestRequest();
    anotherRequest.AddCookie(cookie.Name, cookie.Value);
    anotherRequest.AddHeader("content-type", "application/xml");
    ...
    IRestResponse anotherResponse = client.Execute(anotherRequest);
    

    希望有帮助

    【讨论】:

    • 它给出错误:索引超出范围。必须是非负数且小于集合的大小。参数名称:RestResponseCookie cookie = response.Cookies[0]; 行的索引
    猜你喜欢
    • 1970-01-01
    • 2019-01-15
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 2018-08-11
    • 1970-01-01
    • 2020-03-01
    相关资源
    最近更新 更多