C# || How To Send, Post & Process A REST API Web Request Using C#

Print Friendly, PDF & Email

The following is a module with functions which demonstrates how to send and receive a RESTful web request using C#.

Contents

1. Overview
2. WebRequest - GET
3. WebRequest - POST
4. WebRequest - PUT
5. WebRequest - PATCH
6. WebRequest - DELETE
7. Utils Namespace
8. More Examples


1. Overview

The following functions use System.Net.HttpWebRequest and System.Net.HttpWebResponse to send and process requests. They can be called synchronously or asynchronously. This page will demonstrate using the asynchronous function calls.

The examples on this page will call a test API, and the resulting calls will return Json results.

The Json objects we are sending to the API are hard coded in the examples below. In a real world application, the objects would be serialized first before they are sent over the network, and then deserialized once a response is received. For simplicity, those operations are hard coded.

For examples of how to serialize and deserialize objects using Json and XML, see below:

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. WebRequest – GET

The example below demonstrates the use of ‘Utils.WebRequest.Get‘ to execute a GET request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


3. WebRequest – POST

The example below demonstrates the use of ‘Utils.WebRequest.Post‘ to execute a POST request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


4. WebRequest – PUT

The example below demonstrates the use of ‘Utils.WebRequest.Put‘ to execute a PUT request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


5. WebRequest – PATCH

The example below demonstrates the use of ‘Utils.WebRequest.Patch‘ to execute a PATCH request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


6. WebRequest – DELETE

The example below demonstrates the use of ‘Utils.WebRequest.Delete‘ to execute a DELETE request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


7. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


8. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Was this article helpful?
👍 YesNo

2 Responses to C# || How To Send, Post & Process A REST API Web Request Using C#

  1. Avatar Shane says:

    One issue I’m having with this script is an apparent freezing if the service doesn’t respond. How would I go about setting a timeout value?

    • admin admin says:

      Hi, thanks for visiting!

      Setting a timeout value can be achieved by using the ‘ReadWriteTimeout‘ property in the ‘Utils.WebRequest.Options‘ class

      Hope this helps

Leave a Reply to adminCancel reply