在执行另一个请求之前,我需要处理或删除RestSharp请求吗

本文关键字:请求 处理 删除 RestSharp 另一个 执行 | 更新日期: 2023-09-27 18:29:23

使用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);

在执行另一个请求之前,我需要处理或删除RestSharp请求吗

我认为您应该检查响应是否有效,并为下一个请求保存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);

希望对有所帮助