Introduction
The .NET Framework uses specific classes to provide information required to access Internet resources through a request/response. It also has classes to parse or deserialize the response. To make this short and easy to use, this class library helps us developers to request and get the response and to understand more on how to use HttpWebRequest, HttpWebResponse, JSON.Net and XML Serialization by checking the source code.
Using the Code
Download the attached DLL files and open a new Visual Studio project, add Reference and browse the downloaded attach files or copy and paste the class files HttpRequestResponse.cs and HttpWebBase.cs to your project and import the below namespace:
using HttpWebRequestResponse;
HttpRequestResponse Class
Properties
GetServerResponse - Gets the final response from the server.GetXmlResponseAsXDocument - If the response from server is XML, gets the response as XDocumentHTTP_CONTENT_TYPE - Sets the WebRequest Content Type (default: "application/x-www-form-urlencoded" )HTTP_REQUEST_URI - Sets the WebRequest URIPostRequestString - Sets the WebRequest string BufferUSERNAME_HTTP - Sets the WebRequest Credentials UsernamePASSWORD_HTTP - Sets the basic WebRequest Credentials PasswordPROXY_SERVER - Sets the WebProxy Server AddressPROXY_PORT - Sets the WebProxy Server PortHTTP_REQUEST_METHOD - Sets the WebRequest MethodHTTP_REQUEST_DATE - Sets the WebRequest DateREQUEST_ENCODING - Sets the WebRequest Request encoding type (default: UTF8)
Methods
AddRequestHeader(string Name, object Value) - Sets the collection of header name/value pairs associated with the request.
WriteResponseToDrive(string Path, string FileName) - Writes the server response to text file
For JSON Response
DeserializeJsonResponseAsDataSet() - Deserializes JSON response into DataSetDeserializeJsonResponseAsDataTable() - Deserializes JSON response into DataTableDeserializeJsonResponseAsObject<T>() - Deserializes JSON into ObjectParseJsonElementResponseAsString(string Element) - Returns parse JSON response from server as stringParseJsonResponseAsDictionary(object[] ListSearchKey) - Parses the JSON response base on the array parameter and return Dictionary
For XML response
ParseXmlResponseAsString(string Element) - Returns parse XML response from server element as stringDeserializeXmlResponseAsObject<T>() - Deserializes XML into ObjectParseXmlResponseAsDictionary(object[] ListSearchKey) - Parses XML response based on the Array key object.
Examples
The first code below illustrates how to request and get the response from server:
static void Main(string[] args)
{
string URi = @"http://localhost:4444/api/user/get_user_info?login_name=Jel";
using (HttpRequestResponse HttpWeb = new HttpRequestResponse(URi))
{
HttpWeb.HTTP_REQUEST_METHOD = "GET";
HttpWeb.AddRequestHeader("Authorization", "SAMPLESIGNATURE");
Console.WriteLine(HttpWeb.GetServerResponse); Console.ReadLine();
}
}
Working with JSON response from server using ParseJsonResponseAsDictionary method:
static void Main(string[] args)
{
string URi =
@"http://localhost:4444/api/authentication/check_token?
login_name=Jel&session_token=3fba2165c23e45519b4903cd310f65d7";
using (HttpRequestResponse HttpWeb = new HttpRequestResponse(URi))
{
Dictionary<object, object> oDictionary = new Dictionary<object, object>();
HttpWeb.HTTP_REQUEST_METHOD = "GET";
HttpWeb.AddRequestHeader("Authorization", "SAMPLESIGNATURE");
object[] ListToParse = new object[]
{ "error_code", "message" };
oDictionary = HttpWeb.ParseJsonResponseAsDictionary
(ListToParse); Console.WriteLine(oDictionary["error_code"]);
Console.WriteLine(oDictionary["message"]);
Console.ReadLine();
}
}
Working with JSON response from server using DeserializeJsonResponseAsDataTable method:
static void Main(string[] args)
{
string URi = @"http://localhost:4444/api/user/get_user_info?login_name=Jel";
using (HttpRequestResponse HttpWeb = new HttpRequestResponse(URi))
{
DataTable oTable = new DataTable();
HttpWeb.HTTP_REQUEST_METHOD = "GET";
HttpWeb.AddRequestHeader("Authorization", "SAMPLESIGNATURE"));
oTable = HttpWeb.DeserializeJsonResponseAsDataTable();
DataRow oRow = oTable.Rows[0];
foreach(var item in oRow.ItemArray)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
Working with XMLresponse from server using DeserializeXmlResponseAsObject<T> method:
?>
// <Member>
// <MemberCode>Jelo123</MemberCode>
// <AccountID>1234ID</AccountID>
// <CountryCode>PH</CountryCode>
// </Member>
class Program
{
static void Main(string[] args)
{
string URi = @"http://localhost:3744/WebService.asmx/GetInfo";
using (HttpRequestResponse HttpWeb = new HttpRequestResponse(URi))
{
HttpWeb.HTTP_REQUEST_METHOD = "POST";
HttpWeb.PostRequestBuffer = "MemberName=Jelo";
var oMember = HttpWeb.DeserializeXmlResponseAsObject<Member>();
Console.WriteLine(oMember.MemberCode);
Console.WriteLine(oMember.AccountID);
Console.WriteLine(oMember.CountryCode);
Console.ReadLine();
}
}
}
public class Member
{
public string MemberCode { get; set; }
public string AccountID { get; set; }
public string CountryCode { get; set; }
}
// *** Output ***
// Jelo123
// 123ID
// PH