在C#中使用CURL

时间:2021-11-09 06:27:02

 private string args = ""; 
        /// <summary>
        /// 参数
        /// </summary>
        public string Args { get { return args; } set { args = value; } }

        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param>参数值</param>
        public void SetArg(string value)
        {
            if (string.IsNullOrEmpty(args)) { args = value; } else { args += " " + value; }
        }

        //将cmd的标准输入和输出全部重定向到.NET的程序里
            processStartInfo.UseShellExecute = false;
            processStartInfo.RedirectStandardInput = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.RedirectStandardError = false;
            processStartInfo.CreateNoWindow = true;
            processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        #region 运行
        /// <summary>
        /// 运行
        /// </summary>
        /// <returns></returns>
        public string Run()
        {
            processStartInfo.FileName = Const.CURLPATH;
            processStartInfo.Arguments = args;
            try
            {
                process = Process.Start(processStartInfo);
            }
            catch
            {
                              return process.StandardError.ReadToEnd();
            }
            string resaultValue = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            process.Close();
            return resaultValue;

        }
        #endregion 运行

1. SetArg(" ");  //取得网站内容

2.

--insecure

--request PUT

--basic

-u 用户名:密码

-X IP:端口 代理

...具体查看CURL命令

注意

若要取得CURL自身的错误输出 ,则要使用参数

-stderr "-"

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:11 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:12 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:13 --:--:--     0

上面可使用curlHelper.SetArg("-s --show-error");去除