国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

WebRequest Class

2024-07-21 02:22:02
字體:
供稿:網(wǎng)友


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

}
}
}
 
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泰和县| 平潭县| 静宁县| 碌曲县| 永泰县| 郓城县| 泰兴市| 大足县| 昌宁县| 穆棱市| 万盛区| 土默特右旗| 绥芬河市| 南丹县| 轮台县| 海伦市| 长春市| 江山市| 溆浦县| 潼关县| 浪卡子县| 鹿邑县| 上栗县| 姜堰市| 扎兰屯市| 朝阳区| 奉节县| 吉水县| 德令哈市| 贡嘎县| 远安县| 同德县| 龙井市| 南溪县| 洪洞县| 库伦旗| 科技| 丘北县| 贺州市| 浮梁县| 双江|