Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Functions to Post Data to a URLs for Posting Instructions

0.00/5 (No votes)
25 Sep 2012 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. 

//
// 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 

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here