Niggagay
Smart Home Guru
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
100 XP
This is a simple tutorial in C# on how to use the System.Net namespace to send a request and receive a response from a server.
Now let's start this tutorial!
1. First we need to import System.Net. This enables us to use the required functions and classes.
2. This is the code for request.
To send a request we first create an object of HTTPWebRequest.
HttpWebRequest req =
default
(
HttpWebRequest)
;
We will then have an object equal to a new HTTPWebRequest with the function of '.Create()' while parsing it the server URL.
req =
HttpWebRequest.
Create
(
"http://www.google.com"
)
;
3. This is the code for response.
We will create an object to hold the response. This object is the type of HTTPWebResponse. To initialize, have this code:
HttpWebResponse res =
default
(
HttpWebResponse)
;
Now, we will set the response object to the request's response.
res =
req.
GetResponse
(
)
;
From this we can use that response to do things such as get the source of the response in a web page from the request server.
Full source code:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Now let's start this tutorial!
1. First we need to import System.Net. This enables us to use the required functions and classes.
- using
System.Net
;
2. This is the code for request.
To send a request we first create an object of HTTPWebRequest.
HttpWebRequest req =
default
(
HttpWebRequest)
;
We will then have an object equal to a new HTTPWebRequest with the function of '.Create()' while parsing it the server URL.
req =
HttpWebRequest.
Create
(
"http://www.google.com"
)
;
3. This is the code for response.
We will create an object to hold the response. This object is the type of HTTPWebResponse. To initialize, have this code:
HttpWebResponse res =
default
(
HttpWebResponse)
;
Now, we will set the response object to the request's response.
res =
req.
GetResponse
(
)
;
From this we can use that response to do things such as get the source of the response in a web page from the request server.
- using
(
System.IO
.
StreamReader
reader =
new
System.IO
.
StreamReader
(
res.
GetResponseStream
(
)
)
)
- {
- MessageBox.
Show
(
(
string
)
(
reader.
ReadToEnd
(
)
)
)
;
//Source
- }
Full source code:
- using
System.Net
;
- using
System.Windows.Forms
;
- public
class
Form1
- {
- private
void
Form1_Load(
object
sender, EventArgs e)
- {
- HttpWebRequest req =
default
(
HttpWebRequest)
;
- req =
HttpWebRequest.
Create
(
"http://www.google.com"
)
;
- HttpWebResponse res =
default
(
HttpWebResponse)
;
- res =
req.
GetResponse
(
)
;
- using
(
System.IO
.
StreamReader
reader =
new
System.IO
.
StreamReader
(
res.
GetResponseStream
(
)
)
)
- {
- MessageBox.
Show
(
(
string
)
(
reader.
ReadToEnd
(
)
)
)
;
//Source
- }
- }
- }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware