Skip to main content

How to: Call url request from c#

In C# you can make a request to a web server by using WebRequest. In this blog will be show you some example to use WebRequest object to call a URL.

Make a simple request

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;  
using System.IO;  
using System.Net;  
using System.Text;  

namespace Examples.System.Net  
{  
    public class WebRequestGetExample  
    {  
        public static void Main()  
        {  
            // Create a request for the URL.   
            WebRequest request = WebRequest.Create(  
              "http://www.myserver.com/index.html");  
            // If required by the server, set the credentials.  
            request.Credentials = CredentialCache.DefaultCredentials;  
            // Get the response.  
            WebResponse response = request.GetResponse();  
            // Display the status.  
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);  
            // Get the stream containing content returned by the server.  
            Stream dataStream = response.GetResponseStream();  
            // Open the stream using a StreamReader for easy access.  
            StreamReader reader = new StreamReader(dataStream);  
            // Read the content.  
            string responseFromServer = reader.ReadToEnd();  
            // Display the content.  
            Console.WriteLine(responseFromServer);  
            // Clean up the streams and the response.  
            reader.Close();  
            response.Close();  
        }  
    }  
}  

Submit JSON as POST data


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;  
using System.IO;  
using System.Net;  
using System.Text;  

namespace Examples.System.Net  
{  
    public class WebRequestGetExample  
    {  
        public static void Main()  
        {  
                String url = "http://www.myhost.com/process";

                // Create a request for the URL. 
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

                httpWebRequest.Timeout = 5000;

                // Set ContentType as json
                httpWebRequest.ContentType = "application/json; charset=utf-8";

                // Submit data as POST method
                httpWebRequest.Method = "POST";

                // Write json data into stream
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(jsonData);
                    streamWriter.Flush();

                }

                // Get response from remote server
                HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    string responseText = streamReader.ReadToEnd();
                }
        }  
    }  
}  



Comments

Popular posts from this blog

Serialize and Deserialize JSON data with C#

This post is about example for serialize and deserialize json data with C#. This example using Newtonsoft.Json to be a library to work with json data. You can add Newtonsoft.Json by download from  https://www.newtonsoft.com/json  or using Nuget to add it into your project. In this example is Bill object that hold billing data about Car object. Serialize Object to String Below is example code to serialize object to json string. At line 39 is code to convert object into json string with beautiful json format. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace JSON { class JSonExample { static void Main( string [] args) { List<Car> cars = new List<Car>(); float to...

Laravel 5.6.34 Released

We’ve had an exciting week in Laravel-land, with the launch of Laravel Nova on Wednesday! Tuesday’s release of Laravel 5.6.34 released quietly before Nova, bringing a few changes and a URL validation compatibility fix for PHP 7.3. First up, the whereRowValues changes to properly wrap columns: $builder = DB::table('table')->whereRowValues(['column1', 'column2'], '=', ['foo', 'bar']); dd($builder->toSql()); // expected: select * from "table" where ("column1", "column2") = (?, ?) // actual: select * from "table" where (column1, column2) = (?, ?) Next, the default mail template’s copyright phrase (i.e., “All rights reserved.”) is localizable. Here’s the implementation: @lang('All rights reserved.') The last change was updating the behavior of the EventFake to dispatch non-faked events. The previous functionality was not to execute any event listeners. Now, al...

Install Spring Boot application as a Windows services.

I using  WinSW  to be wrapper for Spring Boot application to run as a Windows service (following section 61.3 of Spring Boot document). There few easy step to setup. Download WinSW binary distribution from website  https://github.com/kohsuke/winsw/releases Copy WinSW.exe into Spring Boot application folder (ex: my file is WinSW.Net4.exe) Rename your WinSW.exe to same as your jar file (for easy to remember). Create XML file name same as jar file. This file is using for configuration of Windows services. Put configuration for services wrapper in your xml file. 1 2 3 4 5 6 7 8 9 <?xml version="1.0" encoding="UTF-8"?> <service> <id>my-application-0.0.1</id> <name>my-application-0.0.1</name> <description>My Exaple Spring Boot Services</description> <executable>java</executable> <arguments>-jar -Xmx1024M -Xms128M "my-application-0.0.1.jar"</arguments> <logmode>rotate...