How to use a webclient | VB.NET 03-15-2013, 02:39 PM
#1
In this tutorial I will show you how to use a webclient. A webclient can be used for many different things, such as receiving the source off of a page. Unlike WebRequests, WebClients only use 2 lines of code and one import.
Lets begin.
Tutorial
Open up your Visual Basic program and create a new project.
For the purpose of this tutorial I will make the name of the project: How to WebClient
Now add 1 button and one RichTextBox. Make the richtextbox very large like this and resize your form.
Example:
![[Image: 2i4s3]](http://puu.sh/2i4s3)
Now what we are going to create is a source grabber, once the button is clicked then the source of the website will be retrieved. This is not as hard as you think it'll be, because all we are doing is telling are webclient to download the page.
Double click your button and then go to the VERY VERY top of your code and enter this code in:
Now what that import does is allow us to use a WebClient without adding extra code.
Then go to the Button_Click event and add this bit of code:
What that does is declare Client as WebClient.
Now to download the page add this code:
What this code does is declare response as a string and then the '=' sign means that response will hold that value in there. What client.downloadstring does is download all of the source on that site. So if we execute the program and press the button, response will contain the value of the page's source.
Now what we want to do is put it into a RichTextBox. This is not hard at all.
All we have to do is add this code:
Now debug the program and click the button and it should download the the site's source.
Making it better
This is not needed, this part of the tutorial is optional, you'll probably learn something from this tutorial, but this is just making your program better.
What we have to do is stay on the project that we were on, for me it is called: How To WebClient.
Add a TextBox
Go to the Button code again and go over to where it says:
Then replace the white text: "http://Anysitehere.com" WITH TextBox1.Text
What this does is, once the button is clicked it'll get all the text from the TextBox, which is suppose to be a website, then open it up in the webclient and download that page.
Now Debug the program, and enter a Http:// website the click the button and see if the page you requested source is there.
I hope I helped you today, expect more tutorials.
Lets begin.
Tutorial
Open up your Visual Basic program and create a new project.
For the purpose of this tutorial I will make the name of the project: How to WebClient
Now add 1 button and one RichTextBox. Make the richtextbox very large like this and resize your form.
Example:
Now what we are going to create is a source grabber, once the button is clicked then the source of the website will be retrieved. This is not as hard as you think it'll be, because all we are doing is telling are webclient to download the page.
Double click your button and then go to the VERY VERY top of your code and enter this code in:
Code:
Imports System.Net
Now what that import does is allow us to use a WebClient without adding extra code.
Then go to the Button_Click event and add this bit of code:
Code:
Dim Client as new WebClient
What that does is declare Client as WebClient.
Now to download the page add this code:
Code:
Dim response as string = client.downloadstring("http://Anysiteyouwanthere.com")
Now what we want to do is put it into a RichTextBox. This is not hard at all.
All we have to do is add this code:
Code:
RichTextBox1.Text = response
Now debug the program and click the button and it should download the the site's source.
Making it better
This is not needed, this part of the tutorial is optional, you'll probably learn something from this tutorial, but this is just making your program better.
What we have to do is stay on the project that we were on, for me it is called: How To WebClient.
Add a TextBox
Go to the Button code again and go over to where it says:
Code:
Dim Response as string = client.downloadstring("http://Anysitehere.com")
What this does is, once the button is clicked it'll get all the text from the TextBox, which is suppose to be a website, then open it up in the webclient and download that page.
Now Debug the program, and enter a Http:// website the click the button and see if the page you requested source is there.
I hope I helped you today, expect more tutorials.