none
Posting using POST from C# over https RRS feed

  • Question

  • Hi All,

    I am trying to do a POST with my below code. It works fine for me if I try to do post on HTTP, but if I try to do the same for HTTPS, I am getting an exception. If I try to post the same on HTTPS by running fiddler tool on my machine it works fine.

    Please help me to tweak my code or tell me if I had missed out something.

    	public string SendRequestToServer(bool isCompressed)
            {
                string postData = this.PostString;
                string response = string.Empty;
                string url = "https://blablabla";
                HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;
                req.AllowWriteStreamBuffering = true;
                req.Method = "POST";
                req.ContentType = "text/xml";
                string frmData = HttpUtility.UrlEncode(postData);
                frmData = "xmldata=" + frmData;
                byte[] formData = UTF8Encoding.UTF8.GetBytes(frmData);
                req.ContentLength = formData.Length;
     
                using (Stream post = req.GetRequestStream())
                {
                    post.Write(formData, 0, formData.Length);
                    post.Close();
                }
     
                using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(resp.GetResponseStream());
                    string resContent=string.Empty;
                    resContent = reader.ReadToEnd();
                    response = resContent;
                }
                return response;
            }

    Thanks in advance.

    Regards,

    Dheeraj.

    Friday, September 7, 2012 7:14 AM

Answers

  • Thanks Joel,

    I got it solved. 

    I used RemoteCertificateValidationCallback attached it to ServicePointManager.ServerCertificateValidationCallback.

    ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateRemoteCertificate);
    
    private static bool ValidateRemoteCertificate(object sender,X509Certificate certificate,X509Chain chain,SslPolicyErrors policyErrors)
    {
        return true;
    }

    Friday, September 7, 2012 12:06 PM

All replies

  • Add CredentialCache.DefaultCredentials (see webpage below).  The credentials will use the same proxy settings as your IE webbrowser (or any browser). Fiddler is probably doing the same thing internally and that is why it works.  When you don't use fiddler you are failing because the URL isn't allowed or you need a certificate to get to the website.

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.credentials(v=VS.71).aspx


    jdweng

    Friday, September 7, 2012 7:26 AM
  • Hi Joel,

    Still no luck.

    I have placed

    req.Credentials = CredentialCache.DefaultCredentials;

    just after initiating request. I got the below exception.

    The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

    Friday, September 7, 2012 9:30 AM
  • Enter the URL into a webbrowser and see if you get an error.  If you do get an error then you need to get the webbrowser working first.  If you don't get an error then it is a header in your file.  I think this line is the problem

    req.ContentType = "text/xml";

    Look at the source page of the URL in your webbrowser by using menu view - source.  Look at the headers in the html file and check the encoding type.  I'm not an expert with the html headers.  there is a negotation that occurs when a connection is made where a common mode of transfer is determined by the client and server negotiating the headers.  Https is using a secure mode of transfer and you are specifying a clear text mode of transfer.


    jdweng

    Friday, September 7, 2012 10:33 AM
  • Joel,

    Im getting error on web browser.

    Is there any way the override the certificate issue through code or configuration?

    Friday, September 7, 2012 11:26 AM
  • Thanks Joel,

    I got it solved. 

    I used RemoteCertificateValidationCallback attached it to ServicePointManager.ServerCertificateValidationCallback.

    ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateRemoteCertificate);
    
    private static bool ValidateRemoteCertificate(object sender,X509Certificate certificate,X509Chain chain,SslPolicyErrors policyErrors)
    {
        return true;
    }

    Friday, September 7, 2012 12:06 PM
  • You should also fix your IE browser settings to allow connections to this site.

    jdweng

    Friday, September 7, 2012 1:18 PM