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