WebException que je ne comprends pas
Bonjour à tous
j'ai besoin de pouvoir piloter mon modem. En premier lieu j'effectue une connexion / déconnexion.
avec le code suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
//partie deconnexion
HttpWebRequest oWRequest = (HttpWebRequest)HttpWebRequest.Create("http://192.168.1.1/apply.cgi");
oWRequest.Method = "POST";
oWRequest.ContentType = "application/x-www-form-urlencoded";
oWRequest.Credentials = new NetworkCredential("admin", "admin");
ASCIIEncoding encoding = new ASCIIEncoding();
encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes("submit_button=submit_button=Status_Router&submit_type=disconnect&change_action=gozila_cgi&wan_proto=dhcp");
oWRequest.ContentLength = loginDataBytes.Length;
Stream stream = oWRequest.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse oWResponse = (HttpWebResponse)oWRequest.GetResponse();
//partie connexion
oWRequest = (HttpWebRequest)HttpWebRequest.Create("http://192.168.1.1/apply.cgi");
oWRequest.Method = "POST";
oWRequest.ContentType = "application/x-www-form-urlencoded";
oWRequest.Credentials = new NetworkCredential("admin", "admin");
ASCIIEncoding encoding = new ASCIIEncoding();
encoding = new ASCIIEncoding();
loginDataBytes = encoding.GetBytes("submit_button=Status_Router&submit_type=connect&change_action=gozila_cgi&wan_proto=dhcp");
oWRequest.ContentLength = loginDataBytes.Length;
stream = oWRequest.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
Exception ici -> HttpWebResponse oWResponse = (HttpWebResponse)oWRequest.GetResponse(); |
ce code fonctionne parfaitement en pas à pas. Mais en exécution normale, il renvoie une WeException et me dit La connexion sous-jacente a été fermée : La connexion a été interrompue de manière inattendue.
Même en plaçant entre les 2 parties une tempo de 25 secondes çe fait la même chose.
Code:
1 2 3 4
|
Thread pause;
pause = System.Threading.Thread.CurrentThread;
pause.Join(25000); |