Click here to Skip to main content
15,881,424 members
Articles / Web Development / ASP.NET
Tip/Trick

Functions to Post Data to a URLs for Posting Instructions

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
25 Sep 2012CPOL1 min read 20.8K   7   1
This article aims to explains how to implement functions to deliver data via HTTP POST or HTTP GET in ASP.NET to an specified link.

Introduction 

This article aims to explains how to implement functions to deliver data via HTTP POST or HTTP GET in ASP.NET to an specified link. This functions can be useful to scrape an external web page automatically from ASP.NET or as a communication protocol for EDI (electronic data interchange) with other sites. 

Background 

Often times in the lead generation industry, we need to deliver data to our clients via HTTP POST or HTTP GET as specified in their posting instructions (instructions that indicate the format in which data must be passed). This article explains how to implement two functions to pass data via HTTP POST and HTTP GET

Using the Code 

The first function we'll be implementing is PostRequest that sends an HTTP POST to an specified URL. We need to pass the data we want to send to the url in the format variable1=value1&variable2=value2...etc.  This function will return the html/json/plain-text or whatever the response from the URL may be. 

C#
//
// ClientData = It's the data we want to pass to the URL
// PostURL = The actual URL we need to pass the data to
 public string PostRequest(string ClientData, string PostURL)
        {

            string responseFromServer = string.Empty;

            try
            {

                // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create(PostURL);
                // Set the Method property of the request to POST.
                request.Method = "POST";
                // Create POST data and convert it to a byte array.


                byte[] byteArray = Encoding.UTF8.GetBytes(ClientData);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
                // Get the response.
                WebResponse response = request.GetResponse();



                if (((HttpWebResponse)response).StatusDescription == "OK")
                {
                    // Get the stream containing content returned by the server.
                    dataStream = response.GetResponseStream();
                    // Open the stream using a StreamReader for easy access.
                    StreamReader reader = new StreamReader(dataStream);
                    // Read the content.
                    responseFromServer = reader.ReadToEnd();

                    // Clean up the streams.
                    reader.Close();
                    dataStream.Close();

                }


                response.Close();



                return responseFromServer;
            }
            catch (Exception)
            {
                return responseFromServer;
            }

        }

The second function we'll be implementing is GetRequestthat sends an HTTP GET to an specified URL. We need to pass the URL in the format url?variable1=value1&variable2=value2; basically, the URL will contain a query string with all the data that needs to be passed. This function will return the html/json/plain-text or whatever the response from the URL may be. You can try calling the function passing it "http://www.google.com" as the PostURL and you'll get the HTML to be rendered by your browser for Google 

C#
public string GetRequest(string PostURL)
        {
            string responseFromServer = string.Empty;
            try
            {
                // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create(PostURL);
                // Set the Method property of the request to POST.
                request.Method = "GET";
                // Create POST data and convert it to a byte array.

                byte[] byteArray = Encoding.UTF8.GetBytes(string.Empty);


                Stream dataStream;
                WebResponse response = request.GetResponse();


                if (((HttpWebResponse)response).StatusDescription == "OK")
                {
                    // Get the stream containing content returned by the server.
                    dataStream = response.GetResponseStream();
                    // Open the stream using a StreamReader for easy access.
                    StreamReader reader = new StreamReader(dataStream);
                    // Read the content.
                    responseFromServer = reader.ReadToEnd();
                    // Clean up the streams.
                    reader.Close();
                    dataStream.Close();
                }

                response.Close();


                return responseFromServer;
            }
            catch (Exception ex)
            {
                return responseFromServer;
            }
        }

Points of Interest 

I use this functions a lot as I work for a lead generation company that is constantly implementing posting instructions for clients to perform EDI.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
United States United States
CS Computer Science/Engineering
MBA

Microsoft Certified Professional Developer (MCPD)
Microsoft Certified IT Professional (MCITP)
Microsoft Certified Technology Specialist (MCTS)

Entrepreneur and engineer with excellent qualifications and strong desired to excel, i'm interested in aligning the IT concepts into the business strategy to propel solutions of complex problems and create more profitable results for any company.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Carsten V2.027-Sep-12 9:37
Carsten V2.027-Sep-12 9:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.