簡體   English   中英

由於保護級別,無法訪問C#命令行

[英]C# Command Line inaccessible due to protection level

我正在嘗試在網頁上進行基於命令行的HTTP發布,該命令行可以在其中登錄和檢索某些數據或執行其他操作。 我已經准備好所有代碼,但是它不喜歡靜態字段Main中的非靜態內容,我該如何解決?

 static void Main(string[] args)
    {
        System.Console.Title = "Test Project";
        bool GoodUsername = false;
        string Username = "";
        while (GoodUsername == false)
        {
            Console.WriteLine("Please enter your username.");
             Username = Console.ReadLine();
            Console.WriteLine("Is " + Username + " correct? Type Yes or No");
            string YesNo = Console.ReadLine();
            if (YesNo == "yes" || YesNo == "Yes" || YesNo == "y")
            {
                GoodUsername = true;
                //return;
            }      
        }
        bool GoodPassword = false;
        string Password = "";           
        while (GoodPassword == false)
        {
            Console.WriteLine("Please enter your password.");
            Password = Console.ReadLine();
            Console.WriteLine("Attempting to log in.");
               string PostURL = "username=" + Username + "&password=" + Password + "&login=Login";
              string URLs = "http://c-rpg.net/index.php?page=login";
             string Response = CRPG.CRPG.DoPost(URLs, PostURL);                       
        }
    }       

是我的代碼來自一類。

 string Response = CRPG.CRPG.DoPost(URLs, PostURL);             

給我錯誤。

 CookieContainer cookies = new CookieContainer();
    protected string DoPost(string URLr, string POST)
    {
        Uri url = new Uri(URLr);
        HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url);

        HttpWRequest.Headers.Set("Pragma", "no-cache");
        HttpWRequest.Timeout = 5000;
        HttpWRequest.Method = "POST";
        HttpWRequest.ContentType = "application/x-www-form-urlencoded";
        HttpWRequest.CookieContainer = cookies;

        byte[] PostData = System.Text.Encoding.ASCII.GetBytes(POST);
        HttpWRequest.ContentLength = PostData.Length;
        Stream tempStream = HttpWRequest.GetRequestStream();
        tempStream.Write(PostData, 0, PostData.Length);
        tempStream.Close();

        HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
        Stream receiveStream = HttpWResponse.GetResponseStream();
        StreamReader readStream = new StreamReader(receiveStream);

        string rcstr = "";
        Char[] read = new Char[256];
        int count = 0;
        while ((count = readStream.Read(read, 0, 256)) > 0)
        {
            rcstr += new String(read, 0, count);
        }
        HttpWResponse.Close();
        readStream.Close();
        return rcstr;
    }

我可以公開它,這給了我錯誤。

Error   1   An object reference is required for the non-static field, method, or property 'CRPG.CRPG.DoPost(string, string)'    c:\users\sales\documents\visual studio 2010\Projects\Test_CL\Test_CL\Program.cs 40

DoPost()cookies字段是實例成員。
您需要該類的實例來調用它們。

您可能想將它們設為靜態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM