首页 > 开发 > 综合 > 正文

WebRequest Class

2024-07-21 02:22:02
字体:
来源:转载
供稿:网友


 
author
date of submission
user level
kareem bawala
04/04/2001
beginner
this is a simple application that the gets the source of a webpage via the webrequest object.

the webrequest class is defined in system.net namespace. webrequest is an abstract class. it is the base class for accessing data from the internet in the .net framework.
this tutorial would show you how to access data on the internet using the webrequest class.
adding namespace reference

since the webrequest class is defined in the system.net namespace, so the first thing to do is to add the system.net reference to the project.

using system.net;

creating the webrequest object

the webrequest object should not be created directly. it is an abstract object. to create the webrequest you should use the webrequestfactory object. the webrequestfactory class is a static class that returns an instance of an object derived from webrequest.

you use the create method of the webrequestfactory class. here is an example:

// you could pass the create method a string or a uri
webrequest wreq = webrequestfactory.create("http://www.yahoo.com");

or

uri uriwebsite = new uri("http://www.yahoo.com");
webrequest wreq = webrequestfactory.create(uriwebsite);

to get the response from the server you call the getresponse() method on the webrequest object.
webresponse wresp = wreq.getresponse();

getting the stream of data from the webresponse object

getting the stream of data you use the getresponsestream() method on the webresponse object and you specify how you want to encode the data you are getting back.
streamreader sr = new streamreader(wresp.getresponsestream(), encoding.ascii);

that's it folks.
namespace htmlsourceviewer {
using system;
using system.net;
using system.text;
using system.io;
using system.winforms;
public class getwebpagesource
{
public getwebpagesource() { }
public getwebpagesource(string webaddress)
{
        this. webaddress = webaddress;
}
public string getsource()
{
       stringbuilder strsource = new stringbuilder("");
try
{
       webrequest wreq = webrequestfactory.create(this.webaddress);
       webresponse wresp = wreq.getresponse();
       // get the stream of data
       streamreader sr = new streamreader(wresp.getresponsestream(), encoding.ascii);
       string strtemp = "";
        while ((strtemp = sr.readline()) != null)
        {
              strsource.append(strtemp + "/r/n");
        }
        sr.close();
}
catch (webexception webexcp)
{
       messagebox.show(webexcp.message, "error", messagebox.iconerror);
}
return strsource.tostring();
}
  
// accessor method
public void setwebaddress(string strnewaddress)
{
this.webaddress = strnewaddress;
}
// private variables
private string webaddress;
}
}
namespace htmlsourceviewer
{
using system;
  
using system.drawing;

using system.collections;
  
using system.componentmodel;
  
using system.winforms;
  
using system.net;
  
using system.net.sockets;
  
using system.runtime.remoting;

public class form1 : system.winforms.form
{
private system.componentmodel.container components;
  
private system.winforms.label label2;
  
private system.winforms.textbox textbox2;
  
private system.winforms.button button2;
  
private system.winforms.button button1;
  
private system.winforms.textbox textbox1;
  
private system.winforms.label label1;
  
private const string http = "http://"; // scheme identifier
  
private string strsource = null;


public form1()

{

initializecomponent();

this.height = 100;

}


public override void dispose()

{

base.dispose();
         
components.dispose();

}

  
private void initializecomponent()

{

this.components = new system.componentmodel.container ();

this.button1 = new system.winforms.button ();

this.label1 = new system.winforms.label ();

this.label2 = new system.winforms.label ();

this.textbox1 = new system.winforms.textbox ();

this.button2 = new system.winforms.button ();

this.textbox2 = new system.winforms.textbox ();

//@this.trayheight = 0;

//@this.traylargeicon = false;
//@this.drawgrid = false;

//@this.trayautoarrange = true;

button1.location = new system.drawing.point (544, 24);

button1.forecolor = system.drawing.color.maroon;

button1.backcolor = system.drawing.color.khaki;

button1.size = new system.drawing.size (88, 24);

button1.tabindex = 2;

button1.text = "get source";

button1.click += new system.eventhandler (this.button1_click);

label1.location = new system.drawing.point (16, 24);

label1.text = "web address";

label1.size = new system.drawing.size (89, 18);

label1.borderstyle = system.winforms.borderstyle.fixedsingle;

label1.autosize = true;

label1.font = new system.drawing.font ("microsoft sans serif", 10);

label1.tabindex = 0;
label1.backcolor = system.drawing.color.khaki;

label2.location = new system.drawing.point (16, 64);

label2.text = "webpage source";

label2.size = new system.drawing.size (114, 18);
label2.borderstyle = system.winforms.borderstyle.fixedsingle;

label2.autosize = true;

label2.tabindex = 5;

label2.anchor = system.winforms.anchorstyles.all;

label2.backcolor = system.drawing.color.khaki;

label2.visible = false;

textbox1.location = new system.drawing.point (112, 24);

textbox1.text = "http://";

textbox1.forecolor = system.drawing.color.maroon;

textbox1.font = new system.drawing.font ("microsoft sans serif", 10);
textbox1.tabindex = 1;

textbox1.size = new system.drawing.size (424, 23);

textbox1.backcolor = system.drawing.color.antiquewhite;

button2.location = new system.drawing.point (640, 24);

button2.forecolor = system.drawing.color.maroon;
button2.backcolor = system.drawing.color.khaki;

button2.size = new system.drawing.size (56, 24);

button2.tabindex = 3;

button2.text = "close";

button2.click += new system.eventhandler (this.button2_click);

textbox2.location = new system.drawing.point (16, 96);

textbox2.multiline = true;

textbox2.borderstyle = system.winforms.
borderstyle.fixedsingle;

textbox2.scrollbars = system.winforms.scrollbars.both;

textbox2.forecolor = system.drawing.color.maroon;

textbox2.tabindex = 4;

textbox2.size = new system.drawing.size (680, 480);

textbox2.backcolor = system.drawing.color.papayawhip;

textbox2.visible = false;

this.text = "kareem's htmlsource viewer";

this.autoscalebasesize = new system.drawing.size (6, 16);

this.keypreview = true;

this.autoscroll = true;

this.font = new system.drawing.font ("microsoft sans serif", 10);

this.backcolor = system.drawing.color.goldenrod;

this.clientsize = new system.drawing.size (704, 581);

this.controls.add (this.label2);

this.controls.add (this.textbox2);

this.controls.add (this.button2);

this.controls.add (this.button1);

this.controls.add (this.textbox1);

this.controls.add (this.label1);

}

protected void button2_click (object sender, system.eventargs e)

{

application.exit();

}

  

protected override void onkeypress(keypresseventargs e)

{

char chr = e.keychar;
//13 = enter key
if (chr == 13)
{
getpagesource();
}

}

  

protected void button1_click (object sender, system.eventargs e)

{    

getpagesource();

}

public void getpagesource()

{

string straddress = textbox1.text.trim();

straddress = straddress.tolower();

  
if ( straddress.length <= http.length)

{
messagebox.show("enter a web address", "web address field", messagebox.iconinformation);
   
textbox1.text = http;
}
  
else if (!straddress.startswith(http))
     
{
     
messagebox.show("you have entered the wrong protocol.", "wrong scheme identifier", messagebox.iconinformation);
textbox1.text = http;

}
else

{

// create the getwebpagesource object

getwebpagesource objgs = htmlsourceviewer.getwebpagesource();
objgs.setwebaddress(straddress);


strsource = objgs.getsource();
if (strsource.length > 1)

{

showsource();

}

}
}
public void showsource()

{

form1.activeform.height = 608;

textbox2.text = strsource;

label2.visible = true;

textbox2.visible = true;

}
public static void main(string[] args)

{

form1 f1 = new form1();

application.run(f1);

}
}
}
 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表