`
lovnet
  • 浏览: 6707451 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

loading a web page using c# (optional use of cookies) - 后台载入网页

阅读更多

http://blog.bitlinkit.com/post/loading-a-web-page-using-c.aspx

This code shows how you can load a web page using c#

make sure to include
using System.Net;
string url = "http://www.bitlinkit.com";
HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url);
//Optional Lines below - for redirects

wRequest.AllowAutoRedirect = false;

wRequest.MaximumAutomaticRedirections = 1;
//Optional lines below - for cookies
static CookieContainer MyCookieContainer = new CookieContainer(); // global static - so that it can remember all cookies
wRequest.CookieContainer = MyCookieContainer;

WebResponse wResponse = wRequest.GetResponse();
System.IO.Stream rStream = wResponse.GetResponseStream();
System.IO.StreamReader sReader = new System.IO.StreamReader(rStream);
//again optional - for cookies
MyCookieContainer = wRequest.CookieContainer;
string TextFromPage = sReader.ReadToEnd();
//----------------------------------------------------------------------------------------
//below lines are optional: use the following to look at cookies as they come in:
foreach (Cookie cook in wResponse.Cookies)
{
Console.WriteLine("Cookie:");
Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
Console.WriteLine("Domain: {0}", cook.Domain);
Console.WriteLine("Path: {0}", cook.Path);
Console.WriteLine("Port: {0}", cook.Port);
Console.WriteLine("Secure: {0}", cook.Secure);

Console.WriteLine("When issued: {0}", cook.TimeStamp);
Console.WriteLine("Expires: {0} (expired? {1})",
cook.Expires, cook.Expired);
Console.WriteLine("Don't save: {0}", cook.Discard);
Console.WriteLine("Comment: {0}", cook.Comment);
Console.WriteLine("Uri for comments: {0}", cook.CommentUri);
Console.WriteLine("Version: RFC {0}", cook.Version == 1 ? "2109" : "2965");

// Show the string representation of the cookie.
Console.WriteLine("String: {0}", cook.ToString());
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics