我可以使用什么来代替 C# 中的 "using" 语句

What could I use instead of the "using" statement in C#

我无法让我的代码正常工作。我需要访问一个只有一行文本的网站,并在 cmd 提示符下显示它。该网站受密码保护,所以我使用 webClient 来存储 cookie,并试图弄清楚如何给它提供用户名和密码。 "using" 语句不起作用,有人知道我可以尝试什么吗?

using System;
using System.Net;

namespace Temperature
{
    public class CookieAwareWebClient : WebClient
    {
        public CookieContainer m_container = new CookieContainer();

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = m_container;
        }
        return request;
        }   

        using (var client = new CookieAwareWebClient());
        var values = new NameValueCollection
        {
            {"username", "admin"},
            {"password","secret"},
        };
    client.UpLoadValues("http://10.10.1.52:8001/get?OID4.3.2.1=", values);

    string tempString = client.DownloadString("http://10.10.1.52:8001/get?OID4.3.2.1=");
    Stream response = myClient.OpenRead("http://10.10.1.52:8001/get?OID4.3.2.1=");
    Console.WriteLine(tempString);
    }
}

您已使用 ; 终止了 using (var client ...),因此无法再访问 client。因此,您需要做的是将其余代码包含在 using block.

using (var client = new CookieAwareWebClient())
{
    var values = new NameValueCollection
    {
        {"username", "admin"},
        {"password","secret"},
    };
    client.UpLoadValues("http://10.10.1.52:8001/get?OID4.3.2.1=", values);
    string tempString = client.DownloadString("http://10.10.1.52:8001/get?OID4.3.2.1=");
    Stream response = myClient.OpenRead("http://10.10.1.52:8001/get?OID4.3.2.1=");
    Console.WriteLine(tempString);
}

注意: 我对 myClient 有点困惑是在 using block 之外声明的拼写错误或任何用途的完整变量。

您没有正确使用 using 块。这个:

using (var client = new CookieAwareWebClient());

和这个基本一样:

using (var client = new CookieAwareWebClient())
{
}

您创建了变量,但随后没有在代码块中对它执行任何操作。所以它立即超出范围。

将您的代码移动到该代码块中以使用您创建的变量:

using (var client = new CookieAwareWebClient())
{
    var values = new NameValueCollection
    {
        {"username", "admin"},
        {"password","secret"},
    };
    client.UpLoadValues("http://10.10.1.52:8001/get?OID4.3.2.1=", values);
    string tempString = client.DownloadString("http://10.10.1.52:8001/get?OID4.3.2.1=");
    // etc.
}

为了理解 using 块的真正作用,它在逻辑上等同于:

try
{
    var client = new CookieAwareWebClient();

    // any code you add to the block
}
finally
{
    client.Dispose();
}

因此,与任何 try 块一样,任何使用在该块内声明的变量的代码也需要在该块内。

你应该这样使用:

    using (var client = new CookieAwareWebClient())
{
        var values = new NameValueCollection
        {
            {"username", "admin"},
            {"password","secret"},
        };
       client.UpLoadValues("http://10.10.1.52:8001/get?OID4.3.2.1=", values);

       string tempString = client.DownloadS`enter code here`tring("http://10.10.1.52:8001/get?OID4.3.2.1=");
        Stream response = myClient.OpenRead("http://10.10.1.52:8001/get?OID4.3.2.1=");
}

因为 Using create a "client" when start and dispose then using block is finished.