I am trying to pass data to an HTML input element on a website using HttpWebRequest and to get back the response of my request with WebResponse.
You will find a screen shot of the form to be filled on internet and a screen shot of the response.
My problem is when running my code ,I always get a response code 500 - internal server error
screen shot of the response on the website
screen shot of input html on the Website
I've updated my code in order to have a good format for my Data (thanks for this)
But I still get the same failure: response code 500 - internal server error.
Here is my updated c# code:
//input html to be field
NameValueCollection Data = new NameValueCollection();
Data.Add("bResultat", "true");
Data.Add("ModeAffichage", "COMPLET");
Data.Add("bImpression", "");
Data.Add("AERO_Date_DATE", "2018/10/03");
Data.Add("AERO_Date_HEURE", "20:50");
Data.Add("AERO_Langue", "FR");
Data.Add("AERO_Duree", "12");
Data.Add("AERO_CM_REGLE", "1");
Data.Add("AERO_CM_GPS", "2");
Data.Add("AERO_CM_INFO_COMP", "1");
Data.Add("AERO_Tab_Aero[0]", "EDDM");
//Create the data in good format
var parameters = new StringBuilder();
foreach (string key in Data.Keys)
{
parameters.AppendFormat("{0}={1}&",
System.Web.HttpUtility.UrlEncode(key),
System.Web.HttpUtility.UrlEncode(Data[key]));
}
parameters.Length -= 1;
byte[] bytedata = Encoding.UTF8.GetBytes(parameters.ToString());
//request to server
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://notamweb.aviation-civile.gouv.fr/Script/IHM/Bul_Aerodrome.php");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bytedata.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(bytedata, 0, bytedata.Length);
reqStream.Close();
MessageBox.Show(bytedata.Length.ToString() + " - " + parameters);
WebResponse response = req.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
I hope you can help me to resolve my problem.