[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
获取cookies
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetGetCookie(string url, string name, StringBuilder data, ref int dataSize);
protected void OPenIEWithUrl(string url)
{
CookieCollection ccl = Cookies.GetCookies(new Uri(url));
if (ccl.Count > 0)
{
string name = ccl[0].Name;
string value = ccl[0].Value + ";expires=Sun,22-Feb-2099 00:00:00 GMT";
InternetSetCookie(url, name, value);
System.Diagnostics.Process.Start("IEXPLORE.EXE", url);// 调用ie打开网页
}
}
/// <summary>
/// 模拟登录
/// </summary>
protected void Login()
{
string loginUrl = "http://hz.fenxiao.uwuxian.com/quickLogin.htm";
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("user.username", "18814887500");
dic.Add("user.password", "123456");
string postData = BuildParmsString(dic);
PostGetData(postData, loginUrl);
OPenIEWithUrl("http://hz.fenxiao.uwuxian.com/");
}
public string PostGetData(string postData, string url)
{
try
{
byte[] data = Encoding.UTF8.GetBytes(postData);
Uri uRI = new Uri(url);
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
req.Method = "POST";
req.KeepAlive = true;
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
req.AllowAutoRedirect = true;
req.CookieContainer = Cookies;
using (Stream outStream = req.GetRequestStream())
{
outStream.Write(data, 0, data.Length);
outStream.Close();
}
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
string htmlResult = string.Empty;
using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
{
htmlResult = sr.ReadToEnd();
}
return htmlResult;
}
catch (Exception ex)
{
return "网络错误:" + ex.Message.ToString();
}
}
private string BuildParmsString(Dictionary<string, string> dicParm)
{
StringBuilder sb = new StringBuilder();
foreach (string key in dicParm.Keys)
{
if (sb.Length > 0)
sb.Append("&");
//sb.AppendFormat("{0}={1}", key, System.Web.HttpUtility.UrlEncode(dicParm[key]));
sb.AppendFormat("{0}={1}", key, dicParm[key]);
}
return sb.ToString();
}