【问题标题】:How to increase timeout when calling webservice in windows phone app?在 windows phone 应用程序中调用 web 服务时如何增加超时?
【发布时间】:2015-09-23 15:34:32
【问题描述】:

我开发了一个 windows phone 应用程序。但是由于超时异常,应用程序崩溃了。如何处理这个超时异常。请帮助我。下面是我的代码:

  public async void Login()
        {


     var client = new NewReloadApp.JsonWebClient();

      var resp = await client.DoRequestAsync(Url.weburl + "Validateuser_v2?Emailid=" + Emailid.Text + "&Password=" + password.Password + "&DeviceID=" + deviceid + "&PlatformID=7&DeviceToken=windowsReload&Mobilemodel=nokia&Appversion=1.4.14&MobileOS=windows");

                string result = resp.ReadToEnd();

                JObject obj = JObject.Parse(result);
}

我没有使用任何 httpwebrequest 方法。在上面的代码中,我尝试设置超时属性,但我无法获得任何超时方法。请帮助我如何为上面的代码设置超时。

下面是我的 jsonwebclient 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace NewReloadApp
{
    class JsonWebClient
    {

        public async Task<T> DoRequestJsonAsync<T>(WebRequest req)
        {
            var ret = await DoRequestAsync(req);
            var response = await ret.ReadToEndAsync();
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(response);
        }

        public async Task<T> DoRequestJsonAsync<T>(string uri)
        {
            var ret = await DoRequestAsync(uri);
            var response = await ret.ReadToEndAsync();
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(response);
        }

        public async Task<System.IO.TextReader> DoRequestAsync(WebRequest req)
        {
            var task = Task.Factory.FromAsync((cb, o) => ((HttpWebRequest)o).BeginGetResponse(cb, o), res => ((HttpWebRequest)res.AsyncState).EndGetResponse(res), req);
            var result = await task;
            var resp = result;
            var stream = resp.GetResponseStream();
            var sr = new System.IO.StreamReader(stream);
            return sr;
        }

        public async Task<System.IO.TextReader> DoRequestAsync(string url)
        {
            HttpWebRequest req = HttpWebRequest.CreateHttp(url);
            req.AllowReadStreamBuffering = true;
            var tr = await DoRequestAsync(req);
            return tr;
        }
    }

}

【问题讨论】:

  • 你可能不得不分享 jsonwebclient 类,或者链接到它(至少我不熟悉它)
  • @zedd 请查看我编辑的代码 我正在分享我的 jsonwebclient 类
  • 公共异步任务 DoRequestAsync(string url) { HttpWebRequest req = HttpWebRequest.CreateHttp(url); req.ContinueTimeout = 30000; req.AllowReadStreamBuffering = true; var tr = 等待 DoRequestAsync(req);返回 tr; }
  • @zedd 我在上面的代码中添加了 req.ContinueTimeout = 30000 延长超时值是否正确

标签: windows-phone-8 windows-phone-8.1 settimeout timeoutexception


【解决方案1】:

使用 CancellationToken:

我想试试,但它可能对你有帮助。

public async Task<System.IO.TextReader> DoRequestAsync(string url)
    {
        CancellationTokenSource ct= new CancellationTokenSource(2000); //2 
        HttpWebRequest req = HttpWebRequest.CreateHttp(url);
        req.AllowReadStreamBuffering = true;
        var tr = await DoRequestAsync(req).AsTask(ct.Token);;
        return tr;
    }

正如post中提到的那样

【讨论】:

  • 感谢您的回复,但它对我不起作用。
  • 我正在使用 .ContinueTimeout 方法是否正确。请帮助我
  • 你可以试试req.timeout
猜你喜欢
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
  • 1970-01-01
相关资源
最近更新 更多