How to increase the request timeout?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 13:59:08

问题


How do I increase the TimeOut for Get or GetTaskAsync? (Using Facebook C# SDK 6)

Seems like they have a timeout of 15 seconds.


回答1:


Use FacebookClient.HttpWebRequestFactory. Since it is hidden from intellisense you might not see it in VS depending on your settings.

    /// <summary>
    /// Http web request factory.
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    public virtual Func<Uri, HttpWebRequestWrapper> HttpWebRequestFactory
    {
        get; set;
    }

You might want to do something like this.

FacebookClient.HttpWebRequestFactory = url => {
  var request = new HttpWebRequestWrapper((HttpWebRequest)WebRequest.Create(url));
  request.Timeout = .....;
  return request;
};

This requires v6 at minimum.

for v5, you need to override a protected method.

    /// <summary>
    /// Creates the http web request.
    /// </summary>
    /// <param name="url">The url of the http web request.</param>
    /// <returns>The http helper.</returns>
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    protected virtual HttpWebRequestWrapper CreateHttpWebRequest(Uri url);



回答2:


For the GetTaskAsync version, you need to pass a cancellation token. The Timeout value set on the request is not honored.

var cancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(2));
await client.GetTaskAsync<T>(path, parameters, cancellationSource.Token);


来源:https://stackoverflow.com/questions/10924610/how-to-increase-the-request-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!