使用 .net (C#) 从curl 记录标准输出时出现问题

发布于 2024-08-23 18:06:25 字数 1545 浏览 7 评论 0原文

我正在尝试将curl 作为一个进程运行并捕获命令窗口的输出。我尝试过直接将curl 作为进程运行,也尝试运行cmd,然后将命令写入命令​​提示符。然而,curl 本身的输出不会被返回(详细模式已打开),尽管我有时会遇到看起来像编码问题的问题,例如 ÈÆŸ。

如果有人有任何灵感,我将不胜感激!

        private static bool ExecuteCurl(string curlDirectory, string curlArgs, string filePath)
        {

            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardError = true;
            process.Start();


            StreamWriter sw = process.StandardInput;
            StreamReader sr = process.StandardOutput;

            sw.WriteLine("cd " + curlDirectory);

            sw.WriteLine("curl " + curlArgs + " -F file=@" + filePath);

            sw.WriteLine("exit");
            sw.Close();

            string cURLResults = string.Empty;
            cURLResults = sr.ReadToEnd();


            sr.Close();

            sw = new StreamWriter("C:\\out.txt", true);
            sw.WriteLine(cURLResults);
            sw.Close();

            return false;

        }

Microsoft Windows XP [版本 5.1.2600] (C) 版权所有 1985-2001 Microsoft Corp.

C:\Dev\VS\bin>cd C:\cURL\

C:\curl>curl -v -k -u xxxxx:xxxxxxx sftp://ftp.xxxx.co.uk -F file=@C:\mydoc.txt

C:\curl>退出

I'm trying to run curl as a process and capture the output from the command window. I have tried both running curl directly as a process and also running cmd and then writing commands to the command prompt. However, the output from curl itself is not being returned (verbose mode is on), although I sometimes get what looks like an encoding issue, e.g. ÈÆŸ.

If anyone has any inspiration I'd be grateful!

        private static bool ExecuteCurl(string curlDirectory, string curlArgs, string filePath)
        {

            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardError = true;
            process.Start();


            StreamWriter sw = process.StandardInput;
            StreamReader sr = process.StandardOutput;

            sw.WriteLine("cd " + curlDirectory);

            sw.WriteLine("curl " + curlArgs + " -F file=@" + filePath);

            sw.WriteLine("exit");
            sw.Close();

            string cURLResults = string.Empty;
            cURLResults = sr.ReadToEnd();


            sr.Close();

            sw = new StreamWriter("C:\\out.txt", true);
            sw.WriteLine(cURLResults);
            sw.Close();

            return false;

        }

Microsoft Windows XP [Version
5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.

C:\Dev\VS\bin>cd C:\cURL\

C:\curl>curl -v -k -u xxxxx:xxxxxxx sftp://ftp.xxxx.co.uk -F file=@C:\mydoc.txt

C:\curl>exit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹎☆浅夏丿初晴 2024-08-30 18:06:25

Curl 似乎将所有输出发送到标准错误,而不是标准输出。

Curl seems to send all output to standard error, not standard out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文