我正在尝试使用 HttpWebRequest 将数据传递到网站上的HTML输入元素,并使用 WebResponse 取回我的请求的响应。
您将找到要在互联网上填写的表单的屏幕截图和响应的屏幕截图。
我的问题是运行代码时,我总是得到响应代码500-内部服务器错误
screen shot of the response on the website
screen shot of input html on the Website
我已经更新了我的代码,以使我的数据具有良好的格式(为此)
但是我仍然遇到相同的失败:响应代码500-内部服务器错误。
这是我更新的c#代码:
//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();
希望您能帮助我解决问题。
答案 0 :(得分:1)
示例代码中有很多地方需要改进。您应该查看IDisposable
和using
块,以确保在您的应用程序使用完资源后正确清理资源。例如,您在关闭请求流之前不会刷新请求流,因此很有可能只有部分请求发送到服务器,从而导致响应500。
HttpRequestMessage
类可帮助您手动处理您在此处执行的许多操作,例如对内容进行格式/ URL编码和设置内容标头。请参阅下面的示例。由于您手动编写请求流的方式,我感到您的代码无法正常工作。如果以下方法不能解决问题,则可能是您的请求数据无效。
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();
答案 1 :(得分:0)
只是告诉您我刚刚获得了第一次成功。 我收到了对我请求的回复。
我在请求中添加了以下参数:
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
我的代码如下:
//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();
我很惊讶地看到Webresponse嵌入了HTML标签。 它需要找到一个过滤器来清洁它。 WebResponse from the HttpWebRequest
感谢您的建议
再见我的下一个问题
西里尔