Jump to content

Ad Block detected  

Welcome To The Home Of
AspectModding

Extra discount

  • Chaotic360 sales are now on
  • 20$ For lifetime premium 
  • Limited time only

AspectModding is currently under maintenance if you discover any bugs and/or errors report them to a member of staff  right away!

To become a beta tester of Chaotic360 Remastered V3.0.0 Message ZZ9 x iHaXoRZz or ZZ9 x iCalZz

  • Announcements

    • ZZ9 x iHaXoRZz

      Chaotic360 Beta Version is now available   12/16/17

      If you are interested in becoming one of our beta testers  you will need the following. Access to Xbox live Have Xbox Live Gold  Have all of the Call Of Duties Ban Bypasses  If you have all of what's needed, then you are one step closer to becoming a Beta Tester, Once you are a Beta Tester you job will be testing all of the options of Chaotic360 old and new, and reporting back anything you believe that could be fixed or improved, this includes slow performance, fatal crashes, console freezing, spelling mistakes etc.  
Sign in to follow this  
distancexd

[c#] HttpWebRequest

Recommended Posts

What is an HttpWebRequest?

  • Whenever your web browser fetches a file (a page, a picture, etc) from a web server, it does so using HTTP - that's "Hypertext Transfer Protocol".  HTTP is a request/response protocol, which means your computer sends a request for some file (e.g. "Get me the file 'home.html'"), and the web server sends back a response ("Here's the file", followed by the file itself).

Request Methods

  • POST
  • GET
  • HEAD
  • PUT
  • DELETE
  • OPTIONS
  • CONNECT

I will be showing you how to use all the request methods inside of c#. But first we need to learn the basics.

First. we need to create a new HttpWebRequest object.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.site.com/login.php");

Next. We need to set the request method, this method can be any of the methods listed above. Note: if you set it to "POST" make sure you also write post data to the request.

request.Method = "POST"

Headers

Headers are a bit weird because there are 2 ways to set them:

request.HeaderName = "HeaderValue";
request.Headers.Add("HeaderName", "HeaderValue");

request.HeaderName = "HeaderValue"; Corresponds to the following headers:

  • Accept
  • Connection
  • Content-Type
  • Date
  • Expect
  • Host
  • Content-Length
  • Referer
  • TE
  • User-Agent
  • Method

 

request.Headers.Add("HeaderName", "HeaderValue"); Corresponds to any headers not specified above.

 

Writing Post Data

Writing post data is needed to write data to the WebRequest whenever the request method is "POST". Note: this is not required with request method other than POST

string data = "username=username&password=password";

byte[] dataBytes = Encoding.ASCII.GetBytes(data);

request.ContentLength = dataBytes.Length;

using (Stream stream = request.GetRequestStream())

{

	stream.Write(dataBytes, 0, dataBytes.Length);

	stream.Close();

}

 

Getting the Response

Once you are done writing post data, or setting the headers you need to send the request to the specified url and get the response back.



HttpWebResponse response = (HttpWebResponse)request.GetResponse();

string responseString = string.Empty;

using (StreamReader reader = new StreamReader(response.GetResponseStream()))

{

	responseString = reader.ReadToEnd();

}

.......

Now that you know the basics of creating a WebRequest. As an example I will send a POST, and GET request to 2 websites. (documentation added)

This is the WebRequest you send. I will be breaking it down and converting it into c# code.

GET http://aspectmodding.net/ HTTP/1.1

Host: aspectmodding.net

Connection: keep-alive

Upgrade-Insecure-Requests: 1

User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

Referer: http://aspectmodding.net/

Accept-Encoding: gzip, deflate, sdch

Accept-Language: en-US,en;q=0.8

Cookie: __cfduid=d9979f163fff28268c362489963d14a701498168864; ips4_IPSSessionFront=p4837m5chs9b5qa6ls4vnpb4q5; ips4_ipsTimezone=America/Tijuana; ips4_hasJS=true
GET http://aspectmodding.net/ HTTP/1.1

This line is pretty simple. It contains the Method, Url and HTTP Version.

> c#

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://aspectmodding.net/");

request.Method = "GET";

---------------

Host: aspectmodding.net

Connection: keep-alive

Upgrade-Insecure-Requests: 1

User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

Referer: http://aspectmodding.net/

Accept-Encoding: gzip, deflate, sdch

Accept-Language: en-US,en;q=0.8

Cookie: __cfduid=d9979f163fff28268c362489963d14a701498168864; ips4_IPSSessionFront=p4837m5chs9b5qa6ls4vnpb4q5; ips4_ipsTimezone=America/Tijuana; ips4_hasJS=true

These lines are all the headers.

> c#

request.Host = "aspectmodding.net";

request.KeepAlive = true;

request.Headers.Add("Upgrade-Insecure-Requests", "1");

request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";

request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";

request.Referer = "http://aspectmodding.net/";

request.Headers.Add("Accept-Language", "en-US,en;q=0.8");

request.Headers.Add("Cookie", "__cfduid=d9979f163fff28268c362489963d14a701498168864; ips4_IPSSessionFront=p4837m5chs9b5qa6ls4vnpb4q5; ips4_ipsTimezone=America/Tijuana; ips4_hasJS=true");

---

And for the response

> c#

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

string responseString = string.Empty;

using (StreamReader reader = new StreamReader(response.GetResponseStream()))

{

	responseString = reader.ReadToEnd();

}

--

Response

866941cb2db5cdbf0488b19e009b5b2a.png

 

Edited by privatebool

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

About us

Welcome to the home of AspectModding where we offer
you your daily mods to your general gossip, if your into
gaming, modding, programming, or just looking for new
friends this is the place for you

Follow us

×