1

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.

Kols
  • 3,641
  • 2
  • 34
  • 42
aviateur22
  • 45
  • 1
  • 8
  • 3
    `Data.ToString()` will return the NameValueCollection's *type name* not the values converted to querystring format. See [this question](https://stackoverflow.com/questions/1667150/is-converting-a-namevaluecollection-to-a-querystring-using-a-c-sharp-lambda-effi) for some conversion methods. – Alex K. Oct 03 '18 at 16:50

2 Answers2

1

There are lots of things to be improved in your example code. You should look at IDisposable and using blocks to ensure resources are properly cleaned up when your application finishes using them. For example, you don't flush your request stream before closing it so there's a strong chance only some of your request is making it to the server which causes the 500 response.

The HttpRequestMessage class helps handle a lot of the things you're doing here manually such as form/url encoding your content and setting the content headers. See my example below. I've got a good feeling your code wasn't working due to the way you were manually writing to your request stream. If the below doesn't fix it, perhaps your request data is invalid.

IEnumerable<KeyValuePair<string, string>> Data = new[] {
    new KeyValuePair<string, string>("bResultat", "true"),
    new KeyValuePair<string, string>("ModeAffichage", "COMPLET"),
    new KeyValuePair<string, string>("bImpression", ""),
    new KeyValuePair<string, string>("AERO_Date_DATE", "2018/10/03"),
    new KeyValuePair<string, string>("AERO_Date_HEURE", "20:50"),
    new KeyValuePair<string, string>("AERO_Langue", "FR"),
    new KeyValuePair<string, string>("AERO_Duree", "12"),
    new KeyValuePair<string, string>("AERO_CM_REGLE", "1"),
    new KeyValuePair<string, string>("AERO_CM_GPS", "2"),
    new KeyValuePair<string, string>("AERO_CM_INFO_COMP", "1"),
    new KeyValuePair<string, string>("AERO_Tab_Aero[0]", "EDDM")
};

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://notamweb.aviation-civile.gouv.fr/Script/IHM/Bul_Aerodrome.php");
request.Content = new FormUrlEncodedContent(Data);

HttpClient cli = new HttpClient();
var response = await cli.SendAsync(request);
var responseText = await response.Content.ReadAsStreamAsync();
Neil
  • 1,613
  • 1
  • 16
  • 18
0

Just to inform you that i just have my first success. I receive a response to my request.

I add the following parameter to my request:

req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";

My code is as follow:

//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/04");
            Data.Add("AERO_Date_HEURE", "21: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");
            Data.Add("AERO_Tab_Aero[1]", "EDDF");
            Data.Add("AERO_Tab_Aero[2]", "EDDL");
            Data.Add("AERO_Tab_Aero[3]", "EDDV");
            Data.Add("AERO_Tab_Aero[4]", "EDDN");

            //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 = WebRequestMethods.Http.Post;
            req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            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'm surprise to see that the Webresponse is embedded with HTML tag. it will need to find a filter to clean it. WebResponse from the HttpWebRequest

Thanks for your advice

and see you for my next question

Cyrille

aviateur22
  • 45
  • 1
  • 8