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 に答える