开发者

How to Set Timeout on WebClient call?

开发者 https://www.devze.com 2023-01-25 12:55 出处:网络
I have the classic: WebClient webClient = ne开发者_运维技巧w WebClient(); webClient.Headers[HttpRequestHeader.ContentType] = \"application/xml\";

I have the classic:

WebClient webClient = ne开发者_运维技巧w WebClient();                    
webClient.Headers[HttpRequestHeader.ContentType] = "application/xml";
webClient.UploadStringAsync(new Uri(inputMessage.Namespace, UriKind.Absolute), inputMessage.ToXML());
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(ProcessResponse)

;

But I want to set the timeout of the request to something lower. My googling directed me to use HttpWebRequest, but i liked the WebClient API. I ended up extending WebClient and overriding a protected method there that would create the underlying HttpWebRequest:

protected override WebRequest GetWebRequest(Uri uri) {
            HttpWebRequest result = WebRequest.CreateHttp(uri);
            result.Timeout = someTimeOut;
            return result;
        }

Problem is in WP7 the HttpWebRequest has no property for setting the Timeout, that is just in normal .NET.

So any ideas?


There's an example of how to implement your own timeout at http://forums.silverlight.net/forums/p/80504/423951.aspx

There is no built-in way to have a timeout.


Use the Microsoft HTTP Client Libraries instead of Silverlight's built-in option.

https://nuget.org/packages/Microsoft.Net.Http/2.1.10


That's by design. The WebClient does not have a timeout property. It's just a wrapper class to the WebRequest...IMO it's just boxing. One of the nice things about the WebClient is that it uses Events and not AsynCallbacks...in the end both Class use the BrowserHttpWebRequest. You could just use the HttpWebRequest and avoid the extension...as you did.

0

精彩评论

暂无评论...
验证码 换一张
取 消