The release number of this article has been CHS307023
This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.
For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.
For Microsoft Visual Basic .NET versions of this article, see
301102.
This task content
Summary
Request a web page full code list
Summary The Microsoft .NET framework contains many classes for the network, including the web request function. This article demonstrates how to make a simple GET request to retrieve the web page from the Internet.
Back to top
The following table summarizes the recommended hardware, software, network architecture, and service pack required:
Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Microsoft Visual Studio .NET
Note: If you are behind the proxy server, you want to test the code in this article, you need an internal web address or static agent value (see steps 7 and 8).
Back to top
Request the web page to programmatically implement a wide range of purposes. Microsoft Visual Basic 6.0 programmers can implement this feature over the Internet Transfer control or directly using the Wininet App Programming Interface (API). In .NET,
The system.net namespace is available.
WebRequest class to encapsulate requests for Internet resources, and provide
The WebResponse class represents the returned data. You can use these objects to get the data stream representing a request response. With the data stream, the operation of the read response is identical to the way to read local files or other resources.
To perform a GET request, follow these steps:
Open Visual Studio .NET to create a new console application in Microsoft C #. Visual C # .NET creates a public class and an empty main () method. Make sure that the project is at least quilt.dll. Using the USING instruction for System, System.Net, and System.IO (for stream object), this is not required to define declarations in these namespaces in the later code. These statements must be placed before all other declarations. Using system;
Using system.net;
Using System.IO; In this example, the Unified Resource Locator (URL) is directly coded as a variable. In actual systems, you may have to use this value as a function of the function, or as a command line parameter of the console application. String surl;
SURL = "http://www.microsoft.com"; New WebRequest object can only be implemented by using a static CREATE method for WebRequest classes ("New WebRequest" is invalid). The target URL is provided as part of the CREATE call to initialize this object using this value. WebRequest WrgetURL;
WrgeTurl = WebRequest.create (SURL); if you are behind the agent, and want to request a URL outside the LAN, you need to create a webproxy object and provide it to the WebRequest object. The WebProxy object has multiple properties (these properties are not set in this presentation), and the basic information specified by these properties is the same as the basic information specified by the proxy settings of Microsoft Internet Explorer. WebProxy myproxy = new webproxy ("MyProxy", 80); myproxy.bypassproxyonlocal = true;
WrgeTurl.Proxy = MyProxy; If you just want to configure it in Internet Explorer, you can implement it through the GetDefaultProxy static method of the WebProxy class. WrgeTurl.Proxy = WebProxy.getDefaultProxy (); After using the target URL and any corresponding proxy information, you can use the request to obtain a Stream object corresponding to the request response. Stream objstream;
ObjStream = Wrgeturl.getResponse (). getResponseStream (); After obtaining the response data stream, you can use it like any other data stream (such as opening a text file), and read its contents in one by one or even all. The following loop statement reads the data stream by line until the READLINE method returns null value and outputs each line to the console. StreamReader Objreader = New StreamReader (ObjStream);
String sline = "";
INT i = 0;
While (sline! = NULL)
{
i ;
Sline = Objreader.Readline ();
IF (sline! = null)
Console.Writeline ("Console.Readline (); saves and runs code, make sure that the agent information is properly configured correctly (steps 7 and 8). At this time, you will see the HTML content with the line number in the console. .
Back to top
Complete code list
Using system;
Using system.net;
Using system.io;
Namespace makeagetRequest_charp
{
///
/// summary description for class1.
/// summary>
Class class1
{
Static void main (string [] args)
{
String surl;
SURL = "http://www.microsoft.com";
WebRequest WrgetURL;
WrgetURL = WebRequest.create (surl);
WebProxy myproxy = new webproxy ("myproxy", 80);
myproxy.bypassproxyonlocal = true;
WrgeTurl.Proxy = WebProxy.getDefaultProxy ();
Stream objstream;
Objstream = WRGETURL.GETRESPONSE (). getResponsestream ();
StreamReader Objreader = new streamreader (objstream); string sline = "";
INT i = 0;
While (sline! = NULL)
{
i ;
Sline = Objreader.Readline ();
IF (sline! = null)
Console.writeline ("Console.Readline ();
}
}
}
Back to top
The information in this article applies to:
Microsoft Visual C # .NET Beta 2
Recent Updated: 2001-11-5 (1.0) Keyword Kbhowto KbhowTomaster KB307023 KBAUDDEVELOPER