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有关,但还是不清楚应该怎么处理?
懂的高手,请不吝赐教!
{
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有关,但还是不清楚应该怎么处理?
懂的高手,请不吝赐教!
感谢你的热心帮助!现在的问题是:我知道访问后续页面的cookie已经在cookiecontainer中。但是,就是不懂怎样利用这个cookiecontainer来写访问后续页面的方法的实现代码啊!另外:如果方便的话贴点代码,或者给点思路,再次谢谢你!
{
//登录成功后再去请求一下后的页面就可以
string source= GetHttpResponse(url,"");
}
不需要对cookie进行处理吗?直接调用string source= HttpHelper.GetHttpResponse(url,"");可以吗?再有,这个调用中的url怎样获得?
比如你要采集csdn个人空间里面的信息 肯定是需要登录的 你登录不是已经做好了吗 那访问的页面就是个人空间的页面了啥 http://my.csdn.net/szzhuyike 你确定你登录已经成功了吧 那就没有其他问题了
很想再接下来的项目完成中,有问题,能得到你的帮助!现在是完成公司的一个小项目,登录博客并发布博客文章。由于自己是新手,真的感觉很棘手!但有csdn热心朋友相助,实在是好啊!