0

我正在调用像 HttpWebRequest 这样的方法,它会给我这样的错误,我的代码是这样的。

HttpWebRequest objHttpWebRequest = System.Net.WebRequest.CreateHttp("http://url");
        objHttpWebRequest.BeginGetResponse(r =>
        {
            WebResponse response = null;
            try
            {
                response = objHttpWebRequest.EndGetResponse(r); //End Async Call to the URL
                using (var stream = response.GetResponseStream())
                using (var reader = new StreamReader(stream)) //get data in StreamReader
                {
                    string contents = reader.ReadToEnd(); //read content from reader and store it in content
                    XElement xmlResult = XElement.Parse(contents);
                    AEGAPI.clsGlobal.RandomToken = xmlResult.Value;
                }

错误快照

我的错误报告是

System.NotSupportedException occurred
  Message=Timeouts are not supported on this stream.
  StackTrace:
       at System.IO.Stream.get_WriteTimeout()
       at MS.Internal.InternalNetworkStream.get_Length()
       at System.IO.StreamReader.ReadToEnd()
       at AEGAPI.clsAEGAPI.<>c__DisplayClass3.<Authenticate>b__0(IAsyncResult r)
       at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
       at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadPool.WorkItem.doWork(Object o)
       at System.Threading.Timer.ring()

谢谢你的帮助 !

4

1 回答 1

1

使用 WebClient.DownloadStringAsync() 会不会更简单?

void startDownload(Uri url)
{
    WebClient wc = new WebClient();
    wc.DownloadStringCompleted += MyMethod;
    wc.DownloadStringAsync(url);
}
void MyMethod(object sender, DownloadStringCompletedEventArgs e)
{
    var contents = e.Result;
    XElement xmlResult = XElement.Parse(contents);
    AEGAPI.clsGlobal.RandomToken = xmlResult.Value;
}
于 2012-05-03T12:46:04.100 回答