C#Webclient超时属性丢失 [英] C# Webclient Timeout Property Missing

查看:483
本文介绍了C#Webclient超时属性丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用webclient,如何设置超时属性。超时属性丢失。

Using the webclient, how do I set the timeout property. The timeout property is missing.

                using (WebClient client = new WebClient())
                {
                    Stream data = client.OpenRead(uri);
                    
                    using (StreamReader reader = new StreamReader(data))
                    {
                        str = reader.ReadToEnd();

                        if (str.Contains("success"))
                            _retvalue = true;
                        else
                            _retvalue = false;
                    }
                }

pianoboyCoder

pianoboyCoder

推荐答案

您通常不会像使用WebClient那样使用它。它旨在轻松获取和发送数据。 

You don't normally use WebClient the way you're using it. It is designed for easily getting and sending data. 

var str = new WebClient().DownloadString(uri);

如果你需要更多的东西,你应该考虑使用
HttpClient

WebRequest
。两者都允许您对呼叫进行更高级的控制,包括超时。

If you need something more than that then you should consider either using HttpClient or WebRequest. Both allow you more advanced control over the call, including the timeout.

Michael Taylor

http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于C#Webclient超时属性丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆