C # simplest Web browser implementation for Network Programming

Source: Internet
Author: User

I recently learned the HTTP protocol programming for C # network programming. HTTP is short for Hypertext Transfer Protocol, meaning Hypertext Transfer protocol.

There are six types of HTTP-related knowledge:

(1). httpwebrequest class: used to obtain and operate HTTP requests;

(2). httpwebresponse class: used to obtain and operate on HTTP responses;

(3). webrequest class: used to obtain and operate Web requests;

(4). webresponse class: used to obtain and operate web responses;

(5). Uri class: used to obtain and operate network resource locations;

(6). WebClient class: used for network client operations.

The following describes how to use the methods described above to obtain the response content of the corresponding URL, assign a value to the data stream, and then read the content from the byte stream and assign it to the webbrowser control to implement the simplest browser, although the content is small, it also reflects some basic browser operations and HTTP-related network programming knowledge.

I. Interface Design

Add label1 "input URL" to the interface, add a textbox as the input URL, and add the text attribute of button1 as "Browse". The most important thing is to add a webbrower control, which indicates "allow users to browse Web pages in the control" and set its anchor attribute (define the control to bind to the container edge) top, bottom, left, and right. In this way, the webpage content can be dynamically changed when you click full screen.

II.Source code

Click the "Browse" button to generate a corresponding Click Event, and add related browsers to the event.CodeAnd add the corresponding namespace.

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. threading. tasks; using system. windows. forms; // Add a new namespace using system. net; using system. io; namespace httpweb {public partial class form1: FORM {public form1 () {initializecomponent () ;}// double-click "Browse" to add the Click Event private void button#click (Object sender, eventargs e) {// obtain the input URL string url = textbox1.text; // create an HTTP link // httpwebrequest object instance: this class is used to obtain and operate HTTP request var. You can change it to httpwebrequest var request = (httpwebrequest) webrequest. create (URL); // create: Create a webrequest object // httpwebresponse object instance: this class is used to obtain and operate the HTTP Response var. You can change it to httpwebresponse var response = (httpwebresponse) request. getresponse (); // getresponse: Get a response // construct a data stream object instance stream = response. getresponsestream (); // getresponsestream: Get the response stream streamreader sr = new streamreader (Stream ); // read characters from the byte stream // read from the current position of the stream to the end and display string content = Sr in the webbrower control. readtoend (); webbrowser1.documenttext = content ;}}}

Iii. Running results

Enter the http://www.baidu.com click "Browse" button to browse Baidu web page, pay attention to add "http: //" in the input URL, it is just a simple implementation, the inevitable processing method is to parse according to the basic syntax.

Click "full screen" to display the following information:

However, there are also some problems during browsing: one is a script error indicating "no permission", and the other is that the display format of some websites such as our internal network is incorrect, we sometimes encounter incorrect formats such as the Intranet. These are the issues that need to be considered next. At the same time, when I browsed Baidu website, I logged on to IE and displayed my username. I used this browser to find that I was not logged on, this confirms that many of our online information is temporarily stored in cookies or sessions, which helps me review the content of previous PHP courses.

Finally, we can use this example to better understand some HTTP network programming knowledge and implement a simple browser. If there are any deficiencies or errors, I hope you can understand it! Hope this articleArticleI can help you and myself. (By: eastmount)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.