欢迎光临
我们一直在努力

WebRequest Class-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

 

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);

thats 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 = "kareems 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);

}
}
}

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » WebRequest Class-.NET教程,Asp.Net开发
分享到: 更多 (0)

相关推荐

  • 暂无文章