110,995
社区成员
发帖
与我相关
我的任务
分享
CookieContainer cookie = new CookieContainer();
UtilsRequest.GetPostCookie("http://www.uu354.com/login.aspx", parms,ref cookie);
richTextBox3.Text = UtilsRequest.GetPost("http://www.uu354.com/Member/DanHaoSearch.aspx", null, cookie);
public static string GetPostCookie(string url, Dictionary<string, object> parms, ref CookieContainer cookie)
{
string htmlCode = String.Empty;
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postData = encoding.GetBytes(GetPostParms(parms));
Uri uri = new Uri(url);
webRequest = System.Net.WebRequest.Create(uri) as HttpWebRequest;
webRequest.Timeout = 30000;
webRequest.Method = "POST";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;
webRequest.CookieContainer = cookie;
webRequest.AllowAutoRedirect = true;
webRequest.KeepAlive = true;
Stream outputStream = webRequest.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
webResponse.Cookies = webRequest.CookieContainer.GetCookies(webRequest.RequestUri);
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), GetEncoding(webResponse)))
{
htmlCode = sr.ReadToEnd();
}
}
catch { }
finally
{
if (webRequest != null) webRequest.Abort();
if (webResponse != null) webResponse.Close();
}
return htmlCode;
}