Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple HttpWebRequest over SSL (https) gives 404 Not Found under C#
    primarykey
    data
    text
    <p>I want to make a POST to a php-written Web Service that sits on a secure connection. The following code is just a test console app I wrote after a few hours of trial and error. Essentially, I found out a few different methods to use HttpWebRequest, but all of them are the same thing. </p> <p>Testing my URI with 'http' on a web browser, should return a blank html (with an empty body). This works OK in a browser <strong>and in my code</strong>.</p> <p>I went ahead a tried <a href="http://www.google.com" rel="nofollow noreferrer">http://www.google.com</a> and I got google… (as expected). The problem arises when I change the URI from http to https. Testing my URI with 'https' on a <strong>web browser</strong>, returns the same blank html (this is expected). But when I try the same URI in code, I get a 404 Not Found. </p> <p>Here's the simple code (and the URI) (uncomment the second one to try https):</p> <pre><code>try { string lcUrl = "http://servicios.mensario.com/enviomasivo/apip/"; //string lcUrl = "https://servicios.mensario.com/enviomasivo/apip/"; // *** Establish the request HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl); // *** Set properties loHttp.Timeout = 10000; // 10 secs loHttp.Method = "POST"; // I added this for testing, but using GET or commenting this out doesn't change anything. // Retrieve request info headers ******** HERE I GET THE EXCEPTION ********** HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse(); // All this code only works when lcUrl is NOT https. Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc); string lcHtml = loResponseStream.ReadToEnd(); loWebResponse.Close(); loResponseStream.Close(); } catch ( WebException ex ) { if ( ex.Status == WebExceptionStatus.ProtocolError ) { HttpWebResponse response = ex.Response as HttpWebResponse; if ( response != null ) { // Process response Console.WriteLine(ex.ToString()); } } } Console.Read(); return; </code></pre> <p>The exception is: </p> <blockquote> <p>System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.GetResponse()</p> </blockquote> <p><strong>note</strong>: The http url shown here is the real one I have to use, it doesn't belong to me but to another company.</p> <p>If the response were OK, this is what lcHtml should contain:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Since I googled and <em>StackOverflowed</em> a <strong>lot</strong> before posting this question, I found out a few ideas. One is to add the "ignore certificates" code:</p> <pre><code>System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate( object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors ) { return true; // **** Always accept }; </code></pre> <p>That doesn't seem to change anything.</p> <p>Other user said that the SSL Protocol Type might be wrong… so I tried with these two to no avail:</p> <pre><code> ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; </code></pre> <p>Any ideas?</p> <p><strong>UPDATE</strong> I went back and created a simple console app. The <em>only</em> code is this:</p> <pre><code>WebRequest req = WebRequest.Create("http://servicios.mensario.com/enviomasivo/apip/"); WebResponse resp = req.GetResponse(); </code></pre> <p>That works. No error.</p> <p>However, if I change the URI to https:</p> <pre><code>WebRequest req = WebRequest.Create("https://servicios.mensario.com/enviomasivo/apip/"); WebResponse resp = req.GetResponse(); </code></pre> <p>I get an error (go ahead and try it).</p> <p><em>However</em> <a href="http://www.mensario.com/SEMPROTP/SDK/saldo.php" rel="nofollow noreferrer">this</a> php code seems to work. The only <strong>relevant</strong> line of code that I see different is:</p> <pre><code>curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); </code></pre> <p>The comment, in spanish says: "With this we can ignore the SSL Cert".</p> <p>I think that's the key. But the </p> <pre><code>System.Net.ServicePointManager.ServerCertificateValidationCallback… </code></pre> <p>…stuff, doesn't seem to have the same effect.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload