Freon
Freon

Reputation: 47

Sending XML over https, Error:The remote certificate is invalid according to the validation procedure

I was trying to write a program which just sends XML from a file over http/s to a web service and then gets a response. The error i get is:

"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure."

Any ideas? Possibly it could the server.

The code below is my program

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();
            }
        }
    }
}

Upvotes: 1

Views: 877

Answers (1)

TTomer
TTomer

Reputation: 356

Try this code:

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();
            }
        }
    }
} 

Upvotes: 1

Related Questions