Generate HTTP Requests using c# (GET/POST)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace MngWebApi.Common
{
public class MyWebRequest
{
private WebRequest request;
private Stream dataStream;

private string status;

public String Status
{
get
{
return status;
}
set
{
status = value;
}
}

public MyWebRequest(string url)
{
// Create a request using a URL that can receive a post.

request = WebRequest.Create(url);
}

public MyWebRequest(string url, string method)
: this(url)
{

if (method.Equals(“GET”) || method.Equals(“POST”))
{
// Set the Method property of the request to POST.
request.Method = method;
}
else
{
throw new Exception(“Invalid Method Type”);
}
}

public MyWebRequest(string url, string method, string data,string header)
: this(url, method)
{

// Create POST data and convert it to a byte array.
string postData = data;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

// Set the ContentType property of the WebRequest.
//request.ContentType = “application/x-www-form-urlencoded”;
request.ContentType = “application/json”;

// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;

if (header != null)
{
request.Headers[“token”] = header;
}

// Get the request stream.
dataStream = request.GetRequestStream();

// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);

// Close the Stream object.
dataStream.Close();

}

public string GetResponse()
{
// Get the original response.
WebResponse response = request.GetResponse();

this.Status = ((HttpWebResponse)response).StatusDescription;

// Get the stream containing all content returned by the requested server.
dataStream = response.GetResponseStream();

// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);

// Read the content fully up to the end.
string responseFromServer = reader.ReadToEnd();

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

return responseFromServer;
}

}
}

Example:

public void AuthenticationTest()
{
try
{

string timestamp = DateTime.Now.ToString(“yyyy-MM-dd hh:mm:ss”);

string hash = Utility.HMAC_SHA(UserName + Password + timestamp + Merchant, key);

string postData = string.Format(“UserName={0}&Password={1}&Merchant={2}&TimeStamp={3}&Hash={4}”, UserName, Password, Merchant, timestamp, hash);

string header = string.Empty;

string adresLink = “http://api.Mobil.com/api/authenticate/authenticate”;

string methodType = “POST”;

MyWebRequest myRequest = new MyWebRequest(adresLink, methodType, postData, header);

//Show Return value
Response.Write(myRequest.GetResponse());

}
catch (Exception ex)
{
Response.Write(ex.Message);
}

}

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: