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();
}; |
Partager