在WP7上使用HttpWebRequest时,Request.EndGetResponse在1分钟后抛出“未找到”错误

时间:2011-09-07 07:05:04

标签: windows-phone-7 httpwebrequest httpwebresponse

我正在使用HttpWebRequest从WP7调用web服务。

在WP7模拟器上一切正常但是当我使用WP7设备时,如果webservice在1分钟内没有响应,它会抛出“Not Found”异常。

我没有找到任何可以增加WP7框架中HttpWebRequest超时的属性。

下面是我正在使用的代码

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            var res = request.BeginGetRequestStream(
               new AsyncCallback((streamResult) =>
               {

                   byte[] requestBytes = Encoding.UTF8.GetBytes(soapRequestEnvelope);

                   try
                   {
                       using (Stream requestStream = request.EndGetRequestStream(streamResult))
                       {
                           requestStream.Write(requestBytes, 0, Encoding.UTF8.GetByteCount(soapRequestEnvelope));
                       }
                   }
                   catch (Exception e)
                   {

                   }

                   request.BeginGetResponse(new AsyncCallback((ar) =>
                   {
                       try
                       {

                           HttpWebRequest Request = (HttpWebRequest)ar.AsyncState;
                           if (Request != null)
                           {
                               //below line throws error if response doesn't come in 1 Minute

                               using (HttpWebResponse webResponse = (HttpWebResponse)Request.EndGetResponse(ar))
                               {
                                  StreamReader reader = new StreamReader(webResponse.GetResponseStream());

                                  //Response comes here
                                  string text =  reader.ReadToEnd();
                               }
                           }
                       }
                       catch (Exception ex)
                       {
                       }

                   }), request);

               }), request);

请帮我解决?

谢谢, SK

1 个答案:

答案 0 :(得分:0)

确保您没有调用本地Web服务,您的设备无法解析localhost而不是模拟器