C# C语言中的Curl请求

C# C语言中的Curl请求,c#,curl,httpwebrequest,webrequest,C#,Curl,Httpwebrequest,Webrequest,我正在尝试将文件内容卷曲到外部源。我可以从命令行正确地卷曲它,但在代码中发送-d开关中的字符串时遇到问题。下面是curl命令的要点 curl.exe -u userName:password -H "Content-Type: text/plain" -X PUT https://someIp/command?filename=filename.txt -d "content of filename.text is here" --insecure 我可以发送文件,他们在另一端接收,问题是文件

我正在尝试将文件内容卷曲到外部源。我可以从命令行正确地卷曲它,但在代码中发送-d开关中的字符串时遇到问题。下面是curl命令的要点

curl.exe -u userName:password -H "Content-Type: text/plain" -X PUT https://someIp/command?filename=filename.txt -d "content of filename.text is here" --insecure
我可以发送文件,他们在另一端接收,问题是文件的内容没有传递给他们。这里有没有人有经验或想法?这是我的概念证明中的代码

  ServicePointManager.ServerCertificateValidationCallback = new
      System.Net.Security.RemoteCertificateValidationCallback
      (
        delegate { return true; }
      );

  // create the requst address to send the file to
  string requestAddress = string.Format("{0}{1}", this.CurlAddress, Path.GetFileName(fileName));

  // spin up the request object, set neccessary paramaters
  var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(requestAddress);
  request.ContentType = "text/plain";
  request.Method = "PUT";
  request.Credentials = new NetworkCredential("userName", "password");      

  // open the web request stream
  using (var stream = request.GetRequestStream())
  {
    // create a writer to the request stream
    using (var writer = new StringWriter())
    {
      // write the text to the stream
      writer.Write(File.ReadAllLines(fileName));

      stream.Close();
    }
  }

  // Get the response
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();

  using (Stream stream = response.GetResponseStream())
  {
    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
    String responseString = reader.ReadToEnd();
    Console.WriteLine(string.Format("Response: {0}", responseString));
  }

我想你想要的是字符串内容。这可以通过HttpClient轻松地异步完成


问题是我没有从requests.GetRequestStream将文件内容写入流。一旦我把内容写在那里,它就会出现在另一端。新的精简代码是

  // open the web request stream
  using (var stream = request.GetRequestStream())
  {
    byte[] file = File.ReadAllBytes(fileName);

    stream.Write(file, 0, file.Length);

    stream.Close();
  }

将HttpClient与PutAsync和StringContent一起使用可能是您的目标。您将StringWriter与请求流关联到哪里?NetMage,我明白您的意思。编写器与请求流没有关联,字符串编写器只是将其写入以太,而不是关闭流。
  // open the web request stream
  using (var stream = request.GetRequestStream())
  {
    byte[] file = File.ReadAllBytes(fileName);

    stream.Write(file, 0, file.Length);

    stream.Close();
  }