I'm facing some issues while developing a C# application which sends some data to a remote PHP script in a server through a POST method, using WebRequest.
My code is as follows:
string url = "https://myserver.com/myfolder/myscript.php";
string PostDataQuery = "q=this+is+a+demo";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse response = null;
Uri aUri = new Uri(url);
string BaseURL_Domain = aUri.GetLeftPart(UriPartial.Authority);
string BaseURL = this.EstandaritzaURL(BaseURL_Domain);
try {
request.ProtocolVersion = System.Net.HttpVersion.Version10;
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate;
request.Proxy = null;
request.Timeout = 10000;
request.ReadWriteTimeout = 4000;
request.MaximumAutomaticRedirections = 3;
request.ServicePoint.Expect100Continue = false;
request.UseDefaultCredentials = true;
request.Host = BaseURL;
request.UserAgent = "Mozilla/5.0 (" + Environment.OSVersion.ToString() + ") " + Application.ProductName + " " + Application.ProductVersion;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//req.Referer = "";
request.Headers.Add("Accept-Language", "ca,es;q=0.7,en;q=0.3");
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.Headers.Add("DNT", "1");
if (method.ToLower().Trim() == "post")
{
//POST
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(PostDataQuery);
request.ContentLength = PostData.Length;
System.IO.Stream postStream = request.GetRequestStream();
postStream.Write(PostData, 0, PostData.Length);
postStream.Flush();
postStream.Close();
}
} catch (Exception e) {
MessageBox.Show(this, "HTTP request error: " + e.Message, "HTTP request error", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
try {
response = (System.Net.HttpWebResponse)request.GetResponse();
if (response == null) return false;
} catch (Exception e) {
if (e.Message.Contains("404"))
MessageBox.Show(this, "HTTP request error: " + e.Message + Environment.NewLine + "Page not found: " + url, "Error 404", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
And I also have the callback to accept the certificate:
public bool HTTPAcceptCertificate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
That very same code works fine in Windows, but when ported to Linux (Mint) it times out in the line:
postStream.Write(PostData, 0, PostData.Length);
If I use HTTP instead of HTTPS, everything works fine. I'm not sure what is going on here. I thought it was a problem related to permissions, but after executing the application through SUDO, the problem persists.
Does anybody have any idea/clue on what to do next?
Thank you very much.
After two days struggling with this now it's fixed. The way to fix this was to upgrade Mono to version 5.8.0.108 from 5.4.1.7, so I presume that I was experiencing some kind of bug.
Thank you very much to the community for your time and effort, you are great!