Construction d'une requete http d'un WebUi
Bonjour,
je cherche à me connecter un webui en local et je n'y arrive pas ça coince.
Il faut que je balance la requete http://localhost:8080/login pour en paramètre username=admin&password=adminadmin.
Mais j'ai l'erreur : le serveur distant retourne une erreur. Merci de votre aide
dans Fiddler, voila ce que je devrais obtenir:
POST http://localhost:8080/login HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 34
Accept: text/javascript, text/html, application/xml, text/xml, */*
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
Content-type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:8080/
Accept-Encoding: gzip, deflate, br
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
username=admin&password=adminadmin
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public static void HttpPostRequest(string url, Dictionary<string, string> postParameters)
{
string postData = "";
foreach (string key in postParameters.Keys)
{
if (postData != "")
postData += "&";
postData += HttpUtility.UrlEncode(key) + "="
+ HttpUtility.UrlEncode(postParameters[key]);
}
HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myHttpWebRequest.Method = "POST";
myHttpWebRequest.KeepAlive = true;
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.ProtocolVersion = HttpVersion.Version11;
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
myHttpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
myHttpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
byte[] data = Encoding.UTF8.GetBytes(postData);
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = data.Length;
Stream requestStream = myHttpWebRequest.GetRequestStream();
Console.WriteLine("\nThe HTTP request Headers for the first request are: \n{0}", myHttpWebRequest.Headers);
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
foreach (Cookie a in myHttpWebResponse.Cookies)
{
Console.WriteLine("{0} {1}", a.Name, a.Value);
}
myHttpWebResponse.Close();
}; |
Code:
1 2 3 4 5 6
|
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("username", "admin");
parameters.Add("password", "adminadmin");
HttpPostRequest("http://localhost:8080/login", parameters); |