tags:

views:

59

answers:

2

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 
+1  A: 

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

dtb
A: 

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

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