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

本文介绍了一种使用C#语言加载网页的方法,并展示了如何通过HttpWebRequest类实例化请求对象,设置请求参数,处理响应流以及读取网页内容。此外,还提供了处理重定向和Cookies的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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());
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值