webclient,webrequest

 

using System;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Security;
public partial class _Default : System.Web.UI.Page
{
// private string URL = "http://laiba.tianya.cn/laiba/CommMsgs?cmm=24373&tid=2630076371579031935";
private string URL = "www.baidu.com";
protected void Page_Load(object sender, EventArgs e)
{
try {

WebClient client = new WebClient();
WebProxy myProxy = new WebProxy("127.0.0.1:1081");
myProxy.Credentials = new NetworkCredential("username", "password");
client.Proxy = myProxy;
Stream data = client.OpenRead(URL);
StreamReader reader = new StreamReader(data);
string str = "";
str = reader.ReadLine();
while (str != null)
{
Response.Write(str);
}
data.Close();
}
catch (WebException exp)
{
Response.Write(exp);
}
}

}
 
webrequest
using System;
using System.Net;
using System.IO;
namespace WebRequestSamp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
WebRequest request = WebRequest.Create(http://www.c-sharpcorner.com/index.asp);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string str = reader.ReadLine();
while(str != null)
{
Console.WriteLine(str);
str = reader.ReadLine();
}
}
}
}

HttpWebRequest and HttpWebResponse classes works in same way too. Here is one sample example.  

Using HttpWebRequest and HttpWebResponse Classes

HttpWebRequest request = (HttpWebRequest)WebRequest.Create (http://www.microsoft.com );
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
String ver = response.ProtocolVersion.ToString();
StreamReader reader = new StreamReader(response.GetResponseStream() );
string str = reader.ReadLine();
while(str != null)
{
Console.WriteLine(str);
str = reader.ReadLine();
}
posted @ 2008-10-23 17:32  looping  阅读(456)  评论(0编辑  收藏  举报