如何在Windows Phone 8中设置RestClient的超时?

时间:2016-02-04 10:04:01

标签: c# windows-phone-8 restsharp

我使用RestSharp API调用Web服务。但是我遇到的问题是,当服务器花费的时间超过RestClient的默认超时(即100秒)时,我总是得到 StatusCode = NotFound 。我也尝试对Timeout和ReadWriteTimeout参数值进行硬编码,但它没有帮助。有人可以建议这里可以做些什么吗?

我的代码如下

public static class RestClientExtensions
{
    private static Task<T> GetResponseFromServer<T>(this RestClient client, IRestRequest request, Func<WebDownloadResult, T> selector)
    {
        var tcs = new TaskCompletionSource<T>();
        WebDownloadResult webResponse = new WebDownloadResult();
        var loginResponse = client.ExecuteAsync(request, r =>
        {

            if (r.ErrorException == null && r.StatusCode == HttpStatusCode.OK)
            {
                webResponse.StatusCode = r.StatusCode;
                webResponse.StatusCodeNumber = (int)r.StatusCode;
                webResponse.Result = r.Content;
                webResponse.ErrorOccured = r.StatusCode == HttpStatusCode.OK ? false : true;
                tcs.SetResult(selector(webResponse));
            }
            else
            {
                tcs.SetException(new Exception(r.StatusCode.ToString()));
            }

        });
        return tcs.Task;
    }

    private static Task<T> GetResponseFromServerForMW<T>(this RestClient client, IRestRequest request, Func<WebDownloadResult, T> selector)
    {
        var tcs = new TaskCompletionSource<T>();
        WebDownloadResult webResponse = new WebDownloadResult();
        var loginResponse = client.ExecuteAsync(request, r =>
        {

            if (r.ErrorException == null && r.StatusCode == HttpStatusCode.OK)
            {
                webResponse.StatusCode = r.StatusCode;
                if (r.Headers.FirstOrDefault(header => header.Name.Equals("P_RET_SYNC_ID")).Value != null)
                {
                    webResponse.AdditionalParameter = r.Headers.FirstOrDefault(header => header.Name.Equals("P_RET_SYNC_ID")).Value;

                }
                webResponse.StatusCodeNumber = (int)r.StatusCode;
                webResponse.Result = r.RawBytes;
                webResponse.ErrorOccured = r.StatusCode == HttpStatusCode.OK ? false : true;
                tcs.SetResult(selector(webResponse));
            }
            else
            {
                tcs.SetException(new Exception(r.StatusCode.ToString()));

            }

        });
        return tcs.Task;
    }

    private static Task<T> GetRawResponseFromServer<T>(this RestClient client, IRestRequest request, Func<WebDownloadResult, T> selector)
    {
        var tcs = new TaskCompletionSource<T>();
        WebDownloadResult webResponse = new WebDownloadResult();
        var loginResponse = client.ExecuteAsync(request, r =>
        {
            if (r.ErrorException == null && r.StatusCode == HttpStatusCode.OK)
            {
                webResponse.StatusCode = r.StatusCode;
                webResponse.StatusCodeNumber = (int)r.StatusCode;
                webResponse.Result = r.RawBytes;
                webResponse.ErrorOccured = r.StatusCode == HttpStatusCode.OK ? false : true;
                tcs.SetResult(selector(webResponse));
            }
            else
            {
                tcs.SetException(new Exception(r.StatusDescription));
            }

        });
        return tcs.Task;
    }

    public static Task<WebDownloadResult> GetResponseAsync(this RestClient client, IRestRequest request)
    {
        return client.GetResponseFromServer(request, r => r);
    }

    public static Task<WebDownloadResult> GetResponseAsyncForMW(this RestClient client, IRestRequest request)
    {
        return client.GetResponseFromServerForMW(request, r => r);
    }

    public static Task<WebDownloadResult> GetResponseBytesAsync(this RestClient client, IRestRequest request)
    {
        return client.GetRawResponseFromServer(request, r => r);
    }


}

其中 CONN_TIMEOUT = 600000 ,我的回复处理类如下:

public class WebDownloadResult
{
    public HttpStatusCode StatusCode { get; set; }
    public string ErrorMessage { get; set; }
    public int StatusCodeNumber { get; set; }
    public bool ErrorOccured { get; set; }
    public object Result { get; set; }
    public object AdditionalParameter { get; set; }
}

WebDownloadResult 如下:

lstResults.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.HeaderSize);
lstResults.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.ColumnContent);

1 个答案:

答案 0 :(得分:0)

您的代码可能正在使用default HttpWebRequest.Timeout

请尝试明确地在RestRequest中传递它 -

request.AddParameter("Timeout", options.Timeout.Value);

您可以尝试更好的版本 -

var client = new RestClient(baseUrl)
{
    Timeout = someTimeoutInMilliseconds
};

var request = new RestRequest(url)
{
    Method = Method.GET,
    Timeout = someTimeoutInMilliseconds
};