如何开始 Stream 以避免两次 web 请求?

How to go to start of Stream to avoid two webrequests?

我有一个小型 C# 控制台应用程序,它可以将 Web 请求的结果复制到一个文本文件,然后运行该文本文件中的每个命令,将结果保存到一个单独的文本文件中。 问题是,我必须向同一台服务器发出两个请求,我不喜欢这样做。问题是在将 Stream/StreamReader 写入文本文件后,我似乎无法转到它的开头,这迫使我提出另一个请求。 我如何只用一个网络请求来做到这一点? 谢谢, 约翰

static void Main(string[] args)
    {
        // Set all variables
        string epoUrl = "https://de-ser2012ecm:8443/remote/core.help.do";
        string commandHelpPath = @"C:\Logs\AllCommandsHelp.txt";
        string coreHelpPath = @"C:\Logs\CoreHelp.txt";
        string epoUsername = "admin";
        string epoPassword = "password";
        string responseFromServer;
        StringReader strReader;

        try
        {
            // Get stream from webrequest
            Stream coreStream = WebHelper.GetWebResponseStream(epoUrl, epoUsername, epoPassword);
            StreamReader coreReader = new StreamReader(coreStream);
            // Write core help page to text file
            using (StreamWriter corefile = new StreamWriter(coreHelpPath, true, Encoding.UTF8))
            {
                responseFromServer = coreReader.ReadToEnd();
                // Display the content.
                corefile.Write(responseFromServer);
                strReader = new StringReader(responseFromServer);
            }

            // Get new stream from webrequest
            Stream commandStream = WebHelper.GetWebResponseStream(epoUrl, epoUsername, epoPassword);
            StreamReader commandReader = new StreamReader(commandStream);

            using (StreamWriter outfile = new StreamWriter(commandHelpPath, true, Encoding.UTF8))
            {
                while (!strReader.Peek().Equals(-1))
                {
                    string streamLine = strReader.ReadLine();
                    string[] words = streamLine.Split(' ');
                    // Check if first string contains a period that's not at the end
                    if ((words[0].Contains(".")) & !(words[0].EndsWith(".")))
                    {
                        StreamReader helpReader = WebHelper.GetWebResponse(epoUrl + "?command=" + words[0], epoUsername, epoPassword);
                        string helpResponseFromServer = helpReader.ReadToEnd();
                        outfile.Write(helpResponseFromServer);
                        outfile.WriteLine("==============================");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Main exception: " + ex.Message);
        }
        finally
        {
            // Close streams
            //coreReader.Close();
            //commandReader.Close();
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
    }

以及 GetWebResponseStream 方法:

public static Stream GetWebResponseStream(string url, string username, string password)
    {
        Stream dataStream = null;

        try
        {
            // Set the credentials.
            CredentialCache credentialCache = new CredentialCache();
            credentialCache.Add(new System.Uri(url), "Basic", new System.Net.NetworkCredential(username, password));
            ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
            // Create a request for the URL. 
            WebRequest request = WebRequest.Create(url);
            request.Credentials = credentialCache;
            // Get the response.
            WebResponse response = request.GetResponse();
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            return dataStream;
        }
        catch (Exception ex)
        {
            Console.WriteLine("GetWebResponse threw an exception: " + ex.Message);
            return dataStream;
        }
    }

感谢 Gildor 的建议,让我走上了正确的道路。我得到了回应,我只需要将该字符串复制到 StringReader 中。这会自动重置光标,以便您可以从顶部开始阅读!我更新了代码以显示新修复。感谢大家的建议。约翰