【发布时间】:2016-07-12 14:24:32
【问题描述】:
我正在尝试编写一个程序,它只是通过 http/s 将文件中的 XML 发送到 Web 服务,然后得到响应。我得到的错误是:
“底层连接已关闭:无法为 SSL/TLS 安全通道建立信任关系。---> System.Security.Authentication.AuthenticationException:根据验证过程,远程证书无效。”
有什么想法吗?可能是服务器。
下面的代码是我的程序
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace XmlSender
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Please enter the URL to send the XML File");
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Console.ReadLine());
byte[] bytes;
Console.WriteLine("Please enter the XML File you Wish to send");
bytes = Encoding.ASCII.GetBytes(Console.ReadLine());
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
}
requestStream.Close();
}
catch (Exception e)
{
Console.WriteLine("An Error Occured" + Environment.NewLine + e);
Console.ReadLine();
}
}
}
}
【问题讨论】: