WP7 HttpWebRequest调用方法出错

时间:2012-05-03 12:32:30

标签: windows-phone-7 c#-4.0 silverlight-4.0 httpwebrequest

我正在调用我的方法,如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;
                }

Error Snap

我的错误报告是

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()

感谢您的帮助!

1 个答案:

答案 0 :(得分: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;
}