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.
public string PostRequest(string ClientData, string PostURL)
{
string responseFromServer = string.Empty;
try
{
WebRequest request = WebRequest.Create(PostURL);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(ClientData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
if (((HttpWebResponse)response).StatusDescription == "OK")
{
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
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
{
WebRequest request = WebRequest.Create(PostURL);
request.Method = "GET";
byte[] byteArray = Encoding.UTF8.GetBytes(string.Empty);
Stream dataStream;
WebResponse response = request.GetResponse();
if (((HttpWebResponse)response).StatusDescription == "OK")
{
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
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.