Files
win8phoneApp/CampusAppWP8/CampusAppWP8ScheduledTaskAgent/Utility/HttpRequest.cs
2013-09-16 19:11:05 +02:00

217 lines
7.7 KiB
C#

//-----------------------------------------------------------------------
// <copyright file="HttpRequest.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>10.06.2013</sience>
//----------------------------------------------------------------------using System;
namespace CampusAppWP8ScheduledTaskAgent.Utility
{
using System;
using System.Collections.Generic;
using System.Net;
using CampusAppWP8ScheduledTaskAgent.Model.Utility;
/// <summary>
/// Class realize the access of restful HttpRequest
/// </summary>
public class HttpRequest
{
#region Member
/// <summary>
/// BaseAddress of the webClient
/// </summary>
private string baseAddress;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="HttpRequest" /> class.
/// </summary>
public HttpRequest()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpRequest" /> class.
/// </summary>
/// <param name="apiBaseAddress">the url of the HttpRequest base address</param>
public HttpRequest(Uri apiBaseAddress)
{
this.baseAddress = apiBaseAddress.AbsoluteUri;
}
#endregion
#region Methods
#region public
/// <summary>
/// Method realize the http-get-method resource
/// </summary>
/// <param name="url">Url of the resource</param>
/// <param name="action">callback method</param>
public void HttpGet(Uri url, Action<object, DownloadStringCompletedEventArgs> action)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(action);
client.DownloadStringAsync(url);
}
/// <summary>Method realize the http-get-method resource.</summary>
/// <remarks>Stubbfel, 03.09.2013.</remarks>
/// <param name="url"> Url of the resource.</param>
/// <param name="action">The action.</param>
public void HttpGet(Uri url, Action<object, OpenReadCompletedEventArgs> action)
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(action);
client.OpenReadAsync(url);
}
/// <summary>
/// Method create the Url for the http-get-method
/// </summary>
/// <param name="parameters"> list of parameters</param>
/// <returns>absolute API-Url include GetParameter</returns>
public Uri CreateGetUrl(List<UrlParamModel> parameters)
{
string paramterStr = string.Empty;
string seperator = string.Empty;
foreach (UrlParamModel parameter in parameters)
{
if (string.Empty.Equals(seperator))
{
seperator = parameter.ParamToken;
}
paramterStr += parameter.ToString();
}
string getUrlStr = this.baseAddress + seperator + paramterStr;
return new Uri(getUrlStr, UriKind.Absolute);
}
/// <summary>
/// Method realize the http-delete-method
/// </summary>
/// <remarks>
/// is not supported by WebClient
/// </remarks>
/// <param name="url">Url of the resource</param>
/// <param name="action">callback method</param>
public void HttpDelete(Uri url, Action<object, DownloadStringCompletedEventArgs> action)
{
throw new NotSupportedException();
}
/// <summary>
/// Method realize the http-head-method
/// </summary>
/// <remarks>
/// is not supported by WebClient
/// </remarks>
/// <param name="url">Url of the resource</param>
/// <param name="action">callback method</param>
public void HttpHead(Uri url, Action<object, DownloadStringCompletedEventArgs> action)
{
throw new NotSupportedException();
}
/// <summary>
/// Method realize the http-options-method
/// </summary>
/// <remarks>
/// is not supported by WebClient
/// </remarks>
/// <param name="url">Url of the resource</param>
/// <param name="action">callback method</param>
public void HttpOptions(Uri url, Action<object, DownloadStringCompletedEventArgs> action)
{
throw new NotSupportedException();
}
/// <summary>
/// Method realize the http-connect-method
/// </summary>
/// <remarks>
/// is not supported by WebClient
/// </remarks>
/// <param name="url">Url of the resource</param>
/// <param name="action">callback method</param>
public void HttpConnect(Uri url, Action<object, DownloadStringCompletedEventArgs> action)
{
throw new NotSupportedException();
}
/// <summary>
/// Method realize the http-trace-method
/// </summary>
/// <remarks>
/// is not supported by WebClient
/// </remarks>
/// <param name="url">Url of the resource</param>
/// <param name="action">callback method</param>
public void HttpTrace(Uri url, Action<object, DownloadStringCompletedEventArgs> action)
{
throw new NotSupportedException();
}
/// <summary>
/// Method realize the http-post-method
/// </summary>
/// <param name="url">Url of the resource</param>
/// <param name="action">callback method</param>
/// <param name="postData">Data which are sending via post to the HttpRequest</param>
public void HttpPost(Uri url, Action<object, UploadStringCompletedEventArgs> action, string postData)
{
this.UploadData(url, action, "POST", postData);
}
/// <summary>
/// Method realize the http-put-method
/// </summary>
/// <param name="url">Url of the resource</param>
/// <param name="action">callback method</param>
/// <param name="putData">Data which are sending via put to the HttpRequest</param>
public void HttpPut(Uri url, Action<object, UploadStringCompletedEventArgs> action, string putData)
{
this.UploadData(url, action, "PUT", putData);
}
/// <summary>
/// Method realize the http-patch-method
/// </summary>
/// <param name="url">Url of the resource</param>
/// <param name="action">callback method</param>
/// <param name="patchData">Data which are sending via patch to the HttpRequest</param>
public void HttpPatch(Uri url, Action<object, UploadStringCompletedEventArgs> action, string patchData)
{
this.UploadData(url, action, "PATCH", patchData);
}
#endregion
#region private
/// <summary>
/// Method uploaded Data to the HttpRequest
/// </summary>
/// <param name="url">Url of the resource</param>
/// <param name="action">callback method</param>
/// <param name="method">name of APIMethod, how the data will be uploaded</param>
/// <param name="data">Data which are sending to the HttpRequest</param>
private void UploadData(Uri url, Action<object, UploadStringCompletedEventArgs> action, string method, string data)
{
WebClient client = new WebClient();
client.UploadStringCompleted += new UploadStringCompletedEventHandler(action);
client.UploadStringAsync(url, method, data);
}
#endregion
#endregion
}
}