编写一个类,需要添加一些引用
using System.Net; using System.Text; using System.IO;
代码如下
public static CookieContainer Cook = new CookieContainer(); public static string CreatRequestGetResponse(string URL, string PostData) { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL); myRequest.CookieContainer = Cook; myRequest.KeepAlive = true; myRequest.Method = "POST"; string postdata = PostData; Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); byte[] postarr = encode.GetBytes(postdata); myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = postarr.Length; Stream outStream = myRequest.GetRequestStream(); outStream.Write(postarr, 0, postarr.Length); outStream.Close(); WebResponse myResponse = myRequest.GetResponse(); StreamReader SReader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8")); return SReader.ReadToEnd(); }