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
return status;
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;
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.


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.

return responseFromServer;



public void AuthenticationTest()

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 = “”;

string methodType = “POST”;

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

//Show Return value

catch (Exception ex)






Leave a Reply

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

You are commenting using your 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: