C# 已经实现模拟登录(可以获得登录后的html),怎样实现打开一个登录后的页面
public class HttpHelper
{
public static CookieContainer Cookies = new CookieContainer();
public static string GetHttpResponse(string url, string postdata)
{
try
{
HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(url);
MyRequest.Method = "POST";
MyRequest.ContentLength = postdata.Length;
MyRequest.CookieContainer = Cookies;
MyRequest.KeepAlive = true;
MyRequest.AllowAutoRedirect = true;
MyRequest.ContentType = "application/x-www-form-urlencoded";
MyRequest.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
MyRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
if (postdata != null)
{
ASCIIEncoding MyEncoding = new ASCIIEncoding();
byte[] MyByte = MyEncoding.GetBytes(postdata);
Stream MyStream = MyRequest.GetRequestStream();
MyStream.Write(MyByte, 0, postdata.Length);
MyStream.Close();
}
string getContent = null;
Encoding myEncoding = Encoding.GetEncoding("UTF-8");
HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();
if (MyResponse.StatusCode == HttpStatusCode.OK)
{
/*获取响应页面cookie*/
// MyResponse.Cookies = MyRequest.CookieContainer.GetCookies(MyRequest.RequestUri);
MessageBox.Show("adfdfasdf");
Stream MyNewStream = MyResponse.GetResponseStream();
StreamReader MyStreamReader = new StreamReader(MyNewStream, myEncoding);
getContent = MyStreamReader.ReadToEnd();
MyStreamReader.Close();
}
MyResponse.Close();
return getContent;
}
catch (Exception)
{
return string.Empty;
}
}
}
上面类代码,已经实现登录,在getContent中返回了html源码,可这个HTML代码怎么显示在网页中
我直接打开一个需要登录后,才能进入的网页。从csdn上看到,这是与cookie有关,但还是不清楚应该怎么处理?
懂的高手,请不吝赐教!
[解决办法]
得到content的html源代码之后,继续用这个方法打开接下来的页面就可以了啊。。这个cookie已经自动存放在你的cookiecontainer中了啊。。
[解决办法]
不好意思 刚刚没有看到你是静态方法
[解决办法]
不需要再对cookie处理什么了 你不是已经自己写了代码保存吗 你再调用这个方法的时候他就自动读取cookie url不是你自己写的吗 就是你登录以后需要访问的页面 要获取的数据
比如你要采集csdn个人空间里面的信息 肯定是需要登录的 你登录不是已经做好了吗 那访问的页面就是个人空间的页面了啥 http://my.csdn.net/szzhuyike 你确定你登录已经成功了吧 那就没有其他问题了