C# 使用客户端证书将xml字符串发布到API

C# 使用客户端证书将xml字符串发布到API,c#,https,get-request,system.net.httpwebrequest,C#,Https,Get Request,System.net.httpwebrequest,我有一个C#(.NET4.7.2)编写的应用程序,用于使用客户端cetificate将xml字符串发布到HTTPSAPI。 这是我的代码: try { ServicePointManager.Expect100Continue = true; ServicePointManager.DefaultConnectionLimit = 9999; ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault;

我有一个C#(.NET4.7.2)编写的应用程序,用于使用客户端cetificate将xml字符串发布到HTTPSAPI。 这是我的代码:

try {
 ServicePointManager.Expect100Continue = true;
 ServicePointManager.DefaultConnectionLimit = 9999;
 ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault;
 ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) {
  return true;
 };
 HttpWebRequest request = null;
 try {
  request = (HttpWebRequest) WebRequest.Create(textBox1.Text);
  result = "200";
 } catch (Exception ex) {}
 if (result.Equals("200")) {
  ASCIIEncoding encoding = new ASCIIEncoding();
  byte[] data = encoding.GetBytes(richTextBox1.Text);
  request.Method = "POST";
  request.ContentType = "application/x-www-form-urlencoded";
  request.Timeout = 10000;
  request.ReadWriteTimeout = 10000;
  request.ProtocolVersion = HttpVersion.Version11;
  X509Certificate2 certificatex = GetX509Certificate(textBox2.Text.Trim(), textBox3.Text.Trim());
  request.ClientCertificates.Add(certificatex);
  request.PreAuthenticate = true;
  request.ContentLength = data.Length;
  try {
   using(var streamWriter = new BufferedStream(request.GetRequestStream())) {
    streamWriter.Write(data, 0, data.Length);
    streamWriter.Flush();
    streamWriter.Close();
   }
  } catch (ProtocolViolationException ex) {

   richTextBox3.Text += "ProtocolViolationException : " + ex.Message + ex.StackTrace + "\n";
   if (ex.InnerException != null) {
    richTextBox3.Text += "ProtocolViolationException.InnerException : " + ex.InnerException.Message + ex.InnerException.StackTrace + "\n";
   }


  } catch (InvalidOperationException ex) {
   richTextBox3.Text += "InvalidOperationException : " + ex.Message + ex.StackTrace + "\n";
   if (ex.InnerException != null) {
    richTextBox3.Text += "InvalidOperationException.InnerException : " + ex.InnerException.Message + ex.InnerException.StackTrace + "\n";
   }

  } catch (NotSupportedException ex) {
   richTextBox3.Text += "NotSupportedException : " + ex.Message + ex.StackTrace + "\n";
   if (ex.InnerException != null) {
    richTextBox3.Text += "NotSupportedException.InnerException : " + ex.InnerException.Message + ex.InnerException.StackTrace + "\n";
   }
  }

  HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  richTextBox2.Text += response.StatusCode.ToString();
 }
} catch (Exception ex) {
 richTextBox3.Text += ex.Message + ex.StackTrace + "\n";

}
当在win 10上运行应用程序时,这是正常的,但当在windows server 2016上运行时,则会出现错误:在InvalidOperationException调用begin getresponse之前,必须将contentlength字节写入请求流


有人能救我吗?非常感谢

请尝试将以http开头的url更改为https(如果可用)。还可以尝试使用Uri.EscapeDataString获取数据,如下所示:encoding.GetBytes(Uri.EscapeDataString(richTextBox1.Text));谢谢@UmutOVECOGLU。它总是https