0

Hi this works when posting to an http address but fails when posting to an HTTPS address with can't estabilish a trust relationship!

What do I need to do to this or is this a server error!?

private static string HttpPost (string uri, string parameters)
{
    //return "ok";
    // parameters: name1=value1&name2=value2    
    try
    {
        WebRequest webRequest = WebRequest.Create(uri);

        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(parameters);
        Stream os = null;
        try
        { // send the Post
            webRequest.ContentLength = bytes.Length;   //Count bytes to send
            os = webRequest.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);         //Send it
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message, "HttpPost: Request error",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            if (os != null)
            {
                os.Close();
            }
        }

        try
        { // get the response
            WebResponse webResponse = webRequest.GetResponse();
            if (webResponse == null)
            { return null; }
            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message, "HttpPost: Response error",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    catch
    {

    }
   return null;
} // end HttpPost 
6
  • 1
    You say it fails... what exception are you getting? If you visit the https .site in a browser does it work properly? Commented Sep 14, 2010 at 20:04
  • BTW: Any reason you're using HttpWebRequest instead of WebClient .UploadValues? Commented Sep 14, 2010 at 20:05
  • System.Net.WebException was caught Message=The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. Source=System Commented Sep 14, 2010 at 20:16
  • I'm using HttpWebRequest as I googled and could not find another way of doing an HTTP Post to a server... Commented Sep 14, 2010 at 20:17
  • @Adrian: WebClient is usually the best option to do HTTP requests, unless you need a specific feature that only HttpWebRequest provides. WebClient.UploadValues does an HTTP POST request. Commented Sep 14, 2010 at 20:22

2 Answers 2

1

The SSL certificate is expired or invalid. Deploy a new, valid SSL certificate on the server.

Sign up to request clarification or add additional context in comments.

Comments

1

Or explicitly trust the cert if that is the right thing to do.

2 Comments

I found this code that works but not sure of the implications or if it will reside after exiting!?
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate(object sender2,X509Certificate certificate, X509Chain chain,SslPolicyErrors sslPolicyErrors) { return true; });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.