日期:2014-05-18  浏览次数:20768 次

HttpWebRequest 登录失败问题--在线等
昨天已发布过的贴子,没解决,今天看看谁有办法,fangxinggood说与.net版本有关,大家有空试试

//登录前获得cookie

string strResult = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://fang.taobao.com/uc/blogin.html?forwardUrl=http%3A%2F%2Ffang.taobao.com");
request.ContentType = "text/html";
request.Method = "GET";
request.Referer = "http://fang.taobao.com/uc/blogin.html?forwardUrl=http%3A%2F%2Ffang.taobao.com";
request.CookieContainer = kbcc;

HttpWebResponse response2 = null;
System.IO.StreamReader sr = null;
response2 = (HttpWebResponse)request.GetResponse();
kbcc.Add(response2.Cookies);
sr = new System.IO.StreamReader(response2.GetResponseStream(), Encoding.GetEncoding("GBK")); //, Encoding.GetEncoding("gb2312") //utf-8
strResult = sr.ReadToEnd();


//正式登录
string postData = "sid=kbnnvoip&password=www123&forwardUrl=http%3A%2F%2Ffang.taobao.com";//参数字符串

  HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://fang.taobao.com/uc/blogin.html");
Encoding encoding = Encoding.GetEncoding("GBK");
byte[] data = encoding.GetBytes(postData.ToString());
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; InfoPath.2; .NET CLR 1.1.4322) ";
myRequest.ContentLength = data.Length;
myRequest.Referer = "http://fang.taobao.com/uc/blogin.html?forwardUrl=http%3A%2F%2Ffang.taobao.com";
myRequest.CookieContainer = kbcc;
myRequest.Timeout = 10000;
myRequest.Accept = "zh-CN";
myRequest.Method = "POST";

Stream newStream = myRequest.GetRequestStream();
Application.DoEvents();
// Send the data.  
newStream.Write(data, 0, data.Length);
newStream.Close();

// Get response  
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
string url = myResponse.ResponseUri.ToString();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("GBK"));
string content = reader.ReadToEnd();
myResponse.Close();


问题:无法登录,还是返回原来的登录页面,我要实现的是登录后可以访问登录后的页面,请大家指教,谢谢!


昨天的贴子:

http://topic.csdn.net/u/20110411/11/f32e090c-0496-49d8-b3af-d5adf9c73357.html?1986479983

------解决方案--------------------