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 asXDocument
HTTP_CONTENT_TYPE
- Sets theWebRequest
Content Type (default: "application/x-www-form-urlencoded
" )HTTP_REQUEST_URI
- Sets theWebRequest
URIPostRequestString
- Sets theWebRequest string Buffer
USERNAME_HTTP
- Sets theWebRequest
Credentials UsernamePASSWORD_HTTP
- Sets the basicWebRequest
Credentials PasswordPROXY_SERVER
- Sets theWebProxy
Server AddressPROXY_PORT
- Sets theWebProxy
Server PortHTTP_REQUEST_METHOD
- Sets theWebRequest
MethodHTTP_REQUEST_DATE
- Sets theWebRequest
DateREQUEST_ENCODING
- Sets theWebRequest
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 intoDataSet
DeserializeJsonResponseAsDataTable()
- Deserializes JSON response intoDataTable
DeserializeJsonResponseAsObject<T>()
- Deserializes JSON intoObject
ParseJsonElementResponseAsString(string Element)
- Returns parse JSON response from server asstring
ParseJsonResponseAsDictionary(object[] ListSearchKey)
- Parses the JSON response base on the array parameter and returnDictionary
For XML response
ParseXmlResponseAsString(string Element)
- Returns parse XML response from server element asstring
DeserializeXmlResponseAsObject<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); // Get the response from server
Console.ReadLine();
}
}
Working with JSON response from server using
ParseJsonResponseAsDictionary
method:// *** Sample JSON response from Server ***
// {"error_code":"OK","message":"Successful"}
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); // execute ParseJsonResponseAsDictionary method
Console.WriteLine(oDictionary["error_code"]);
Console.WriteLine(oDictionary["message"]);
Console.ReadLine();
}
}
// *** Output ***
// OK
// Successful
Working with JSON response from server using
DeserializeJsonResponseAsDataTable
method:// *** Sample JSON response from Server ***
// [{"error_code":"OK","message":"Successful","Name":"Jel",
// "Lastname":"TheLastName"}]
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();
}
}
// *** Output ***
// OK
// Successful
// Jel
// TheLastName
Working with
XMLresponse
from server using DeserializeXmlResponseAsObject<T>
method:// *** Sample XML response from Server ***
// <?xml version="1.0" encoding="utf-8"?>
// <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
Source collected from :http://www.codeproject.com/Tips/830018/Class-Library-that-uses-HttpWebRequest-and-HttpWeb
No comments :
Post a Comment