• We just launched and are currently in beta. Join us as we build and grow the community.

Request and Response in C#

Niggagay

Smart Home Guru
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
126
Likes
32
Bits
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.
  1. 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.

  1. using

    (

    System.IO

    .

    StreamReader

    reader =

    new

    System.IO

    .

    StreamReader

    (

    res.

    GetResponseStream

    (

    )

    )

    )
  2. {
  3. MessageBox.

    Show

    (

    (

    string

    )

    (

    reader.

    ReadToEnd

    (

    )

    )

    )

    ;

    //Source
  4. }


Full source code:
  1. using

    System.Net

    ;
  2. using

    System.Windows.Forms

    ;


  3. public

    class

    Form1
  4. {

  5. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  6. {
  7. HttpWebRequest req =

    default

    (

    HttpWebRequest)

    ;
  8. req =

    HttpWebRequest.

    Create

    (

    "http://www.google.com"

    )

    ;
  9. HttpWebResponse res =

    default

    (

    HttpWebResponse)

    ;
  10. res =

    req.

    GetResponse

    (

    )

    ;
  11. using

    (

    System.IO

    .

    StreamReader

    reader =

    new

    System.IO

    .

    StreamReader

    (

    res.

    GetResponseStream

    (

    )

    )

    )
  12. {
  13. MessageBox.

    Show

    (

    (

    string

    )

    (

    reader.

    ReadToEnd

    (

    )

    )

    )

    ;

    //Source
  14. }

  15. }
  16. }

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

 

452,292

323,526

323,535

Top