淘宝模拟登录解决完,本机测试可以,可是放到服务器上不行,不知道是不是跟服务器版本有关,windows2003 R2 sp2可以,windows2003sp2不行.
以后再换服务器,一定要换windows2008的了,跟不上微软的步伐真不好。要么不跟他混了。
模拟登录用到了HttpWebRequest,CookieContainer的使用是关键。
贴段代码:
CookieContainer cookieContainer = new CookieContainer(); string formatString = "TPL_username={0}&TPL_password={1}" + urlCatalog;//*************** string postString = string.Format(formatString, username, pwd); //将提交的字符串数据转换成字节数组 byte[] postData = Encoding.UTF8.GetBytes(postString); //设置提交的相关参数 string URI = "https://login.taobao.com/member/login.jhtml";//*************** HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest; request.Method = "POST"; request.KeepAlive = true; request.ContentType = "application/x-www-form-urlencoded"; request.CookieContainer = cookieContainer; request.ContentLength = postData.Length; request.AllowAutoRedirect = false; //cookieheader = request.CookieContainer.GetCookieHeader(new Uri(URI)); //List<Cookie> CookieList = GetAllCookies(cookieContainer); // 提交请求数据 System.IO.Stream outputStream = request.GetRequestStream(); outputStream.Write(postData, 0, postData.Length); outputStream.Close(); //接收返回的页面,必须的,不能省略 HttpWebResponse response = request.GetResponse() as HttpWebResponse; System.IO.Stream responseStream = response.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("GB2312")); string srcString = reader.ReadToEnd(); //cookieContainer = InitCookie(cookieContainer); //Response.Write(srcString); //CookieCollection myCookies = null; //myCookies = response.Cookies; //string cookie1 = response.Headers.Get("Set-Cookie"); reader.Close(); responseStream.Close(); request.Abort(); response.Close();