0

我正在写一个MVC Web API使异步HttpWebRequest调用。我得到2个不同的例外。以下是我正在使用的方法。2异常时尝试使异步HttpWebRequest

第一个例外是:“此流不支持查找操作。”它发生在responseStream上。

第二个例外是:“此流上不支持超时”,这种情况发生在MemoryStream内容上。

我在做什么错?我一直在谷歌搜索,但没有真正找到任何解决方案。

感谢,

朗达

enter image description here

enter image description here 私人异步任务GetHtmlContentAsync(字符串requestUri,字符串的userAgent,串引用,布尔的keepAlive,时间跨度超时,BOOL forceTimeoutWhileReading,串代理字符串requestMethod ,字符串类型) { //保留的字符串响应 string output = null;

 //create request object 
     var request = (HttpWebRequest)WebRequest.Create(requestUri); 
     var content = new MemoryStream(); 
     request.Method = requestMethod; 
     request.KeepAlive = keepAlive; 
     request.Headers.Set("Pragma", "no-cache"); 
     request.Timeout = (Int32)timeout.TotalMilliseconds; 
     request.ReadWriteTimeout = (Int32)timeout.TotalMilliseconds; 
     request.Referer = referrer; 
     request.Proxy = new WebProxy(proxy); 
     request.UserAgent = userAgent; 

     try 
     { 
      using (WebResponse response = await request.GetResponseAsync().ConfigureAwait(false)) 
      { 
       using (Stream responseStream = response.GetResponseStream()) 
       { 
        if (responseStream != null) 
        { 
         await responseStream.CopyToAsync(content); 
        } 
       } 

       var sr = new StreamReader(content); 
       output = sr.ReadToEnd(); 
       sr.Close(); 
      } 
     } 
     catch (Exception ex) 
     { 
      output = string.Empty; 
      var message = ("The API caused an exception in the " + type + ".\r\n " + requestUri + "\r\n" + ex); 
      Logger.Write(message); 
     } 

     return output; 
    } 
+0

什么是您的例外堆栈帧? –

+0

我认为你的意思是堆栈跟踪? – Rhonda

+0

是的,堆栈跟踪。 –

回答

0

我通过之前新的StreamReader线添加

content.Position = 0

固定的问题。现在我只需要让它与GZip压缩一起工作。

Rhonda