如何使用'out'和'out'参数

Cra*_*cyD 0 c# timeout using out httpwebresponse

我有一个像这样的方法

private bool VerbMethod(string httpVerb, string methodName, string url, string command, string guid, out HttpWebResponse response)
Run Code Online (Sandbox Code Playgroud)

我这样使用它

  HttpWebResponse response;
  if (VerbMethod("POST", "TheMethod", "http://theurl.com", "parameter1=a", theGuid, out response))
  {
    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
    {
      string responseString = sr.ReadToEnd();
    }
Run Code Online (Sandbox Code Playgroud)

它返回一个bool来指定​​方法是否顺利,并在out参数中设置响应以获取数据.

我有时会超时,然后后续请求也会超时.我看到这个SO WebRequest.GetResponse锁定了吗?

它重新启动using关键字.问题是,用上面的方法签名我不知道该怎么做.

  • 我应该手动调用最终处理吗?
  • 有没有一些方法仍然使用usingout参数?
  • 重写方法,所以它不暴露HttpWebResponse

Jon*_*eet 6

它返回一个bool来指定​​方法是否顺利

那是你的问题.不要使用布尔成功值:如果出现问题则抛出异常.(或者说,让异常泡沫化.)

只需更改方法即可返回响应.