2

I found that this code works as expected:

var url = "https://limal.info/efulfilment.php";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var alternativeAnswer = Encoding.UTF8.GetString(new WebClient().UploadValues(url, new NameValueCollection() { { "xml", "test" } }));

The following code however gives me a headache:

var url = "https://limal.info/efulfilment.php";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

var request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Timeout = 5000; // I added this for you, so you only need to wait 5 sec...

using (var requestStream = request.GetRequestStream())
{
    var writer = new StreamWriter(requestStream);
    writer.Write("xml=test");
}
using (var response = request.GetResponse())
{
    using (var responseStream = response.GetResponseStream())
    {
        var reader = new StreamReader(responseStream);
        var answer = reader.ReadToEnd();
    }
}

Somehow the post parameters don't get recognized and I get the response:

"limal.info bridge error: Missing 'xml' variable in post request."

(The correct answer would be that the XML data is wrongly formatted, as test is invalid XML...)

Now to the next Problem:

When I use a different url a timeout exception occurs. It hangs at UploadValues in the following code. (The other example that uses HttpWebRequest hangs at GetResponse, which I tried, too.)

var url = "https://sys.efulfilment.de/rt/";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var alternativeAnswer = Encoding.UTF8.GetString(new WebClient().UploadValues(url, new NameValueCollection() { { "xml", "test" } }));

I read about similar problems here and on other sites. It seems that using Http POST with SSL is a problem in .NET.

WHY?? :(

Community
  • 1
  • 1
NanoWar
  • 81
  • 1
  • 5
  • Problem 1 solved: I echoed the HTTP request and saw that it was empty. It appears, that the StreamWriter only actually writes when you flush it. writer.Write("xml=test"); writer.Flush(); – NanoWar Jun 06 '12 at 13:54
  • Re Problem 1. The StreamWriter should be created in a using block too, that will cause the flush. It will also close the Stream too; but maybe just leave the Stream using block in the code for readability. – alanjmcf Jun 12 '12 at 08:23
  • Problem 2 gets solved here: http://blogs.msdn.com/b/fiddler/archive/2012/03/29/https-request-hangs-.net-application-connection-on-tls-server-name-indicator-warning.aspx – NanoWar Jun 22 '12 at 23:30

0 Answers0