【问题标题】:Sending XML over https, Error:The remote certificate is invalid according to the validation procedure通过 https 发送 XML,错误:根据验证程序,远程证书无效
【发布时间】: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();
            }
        }
    }
}

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    试试这个代码:

    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.UTF8.GetBytes(Console.ReadLine());
    request.Headers.Add("Content-Encoding", "utf-8");
                    request.ContentLength = bytes.Length;
    request.UserAgent = @"Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";
                    request.Method = "POST";
                    request.Timeout = 20000;
                    request.KeepAlive = false;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(bytes, 0, bytes.Length);
                    }
    
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        Stream responseStream = response.GetResponseStream();
                        string responseStr = new StreamReader(responseStream).ReadToEnd();
                    }
    
                }
                catch (Exception e)
                {
                    Console.WriteLine("An Error Occured" + Environment.NewLine + e);
                    Console.ReadLine();
                }
            }
        }
    } 
    

    【讨论】:

    • 好吧错误更改为:底层连接已关闭:接收时发生意外错误。 ---> System.IO.IOException: Unable to read data from the transport connection: 一个现有的连接被远程主机强行关闭。 ---> System.Net.Sockets.SocketException: 现有连接被远程主机强行关闭
    • 我猜网络服务正在关闭连接?因为某事
    • 你为什么在while(true)?尝试将 requestStream.close() 移动到响应读取后
    • 好吧,删除了 while true 因为我试图发送多个 xml 数据并接收每个数据的响应。但是出现了同样的错误。
    • 尝试用您的相关部分替换它:request.Timeout = 20000;使用 (Stream requestStream = request.GetRequestStream()) { requestStream.Write(bytes, 0, bytes.Length); } 使用 (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { 流 responseStream = response.GetResponseStream();字符串 responseStr = new StreamReader(responseStream).ReadToEnd(); }
    猜你喜欢
    • 2016-12-12
    • 1970-01-01
    • 2011-03-28
    • 2016-11-28
    • 2013-08-08
    • 2012-07-20
    • 2015-03-17
    相关资源
    最近更新 更多