Last active
August 29, 2015 14:01
-
-
Save sandromello/b7baf0c98da22e6bc5bb to your computer and use it in GitHub Desktop.
Example of http request C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Net; | |
using System.Collections; | |
namespace MailApiModule { | |
public static class LocamailHttpRequest { | |
public static LocamailHttpResponse DoRequest(string url, Method method, object serializedData) { | |
HttpWebRequest wb = WebRequest.Create(url) as HttpWebRequest; | |
if (LocamailRequest.SERVICETICKET != null) | |
wb.Headers["Service-Ticket"] = LocamailRequest.SERVICETICKET; | |
if (LocamailRequest.IGNORESSLERRORS) | |
ServicePointManager.ServerCertificateValidationCallback | |
= ((sender, certificate, chain, sslPolicyErrors) => true); | |
wb.Method = method.ToString(); | |
wb.AllowAutoRedirect = false; | |
if (method != Method.GET) { | |
string postData; | |
// If is a primitive, then is a string otherwise must encode for post/put/delete request | |
if (serializedData is string) { | |
wb.ContentType = "application/json"; | |
postData = serializedData as string; | |
Console.WriteLine("POSTDATA application/json: " + postData); | |
} else { | |
wb.ContentType = "application/x-www-form-urlencoded"; | |
postData = UrlHelpers.UrlEncode(serializedData); | |
Console.WriteLine("POSTDATA x-www-form: " + postData); | |
} | |
byte[] data = Encoding.UTF8.GetBytes(postData); | |
wb.ContentLength = data.Length; | |
// Write to request body if isn't a GET request | |
using (System.IO.Stream fStream = wb.GetRequestStream()) { | |
fStream.Write(data, 0, data.Length); | |
} | |
} else { | |
//Debug | |
Console.WriteLine("Serialized DATA is NULL. Method: " + method.ToString()); | |
} | |
LocamailHttpResponse httpResponse = null; | |
HttpWebResponse response = null; | |
try { | |
response = wb.GetResponse() as HttpWebResponse; | |
httpResponse = new LocamailHttpResponse(response); | |
} catch (WebException w) { | |
if (w.Response == null) | |
throw; | |
// Resposta da requisicao | |
response = w.Response as HttpWebResponse; | |
httpResponse = new LocamailHttpResponse(response); | |
} finally { | |
if (response != null) | |
response.Close(); | |
} | |
return httpResponse; | |
} | |
} | |
public enum Method { | |
POST, | |
PUT, | |
GET, | |
DELETE | |
} | |
public static class UrlHelpers { | |
public static string UrlEncode(this object request, string separator = ",") { | |
if (request == null) | |
throw new ArgumentNullException("request"); | |
// Get all properties on the object | |
var properties = request.GetType().GetProperties() | |
.Where(x => x.CanRead) | |
.Where(x => x.GetValue(request, null) != null) | |
.ToDictionary(x => x.Name, x => x.GetValue(request, null)); | |
// Get names for all IEnumerable properties (excl. string) | |
var propertyNames = properties | |
.Where(x => !(x.Value is string) && x.Value is IEnumerable) | |
.Select(x => x.Key) | |
.ToList(); | |
// Concat all IEnumerable properties into a comma separated string | |
foreach (var key in propertyNames) { | |
var valueType = properties[key].GetType(); | |
var valueElemType = valueType.IsGenericType | |
? valueType.GetGenericArguments()[0] | |
: valueType.GetElementType(); | |
if (valueElemType.IsPrimitive || valueElemType == typeof(string)) { | |
var enumerable = properties[key] as IEnumerable; | |
properties[key] = string.Join(separator, enumerable.Cast<object>()); | |
} | |
} | |
// Concat all key/value pairs into a string separated by ampersand | |
return string.Join("&", properties | |
.Select(x => string.Concat( | |
Uri.EscapeDataString(x.Key), "=", | |
Uri.EscapeDataString(x.Value.ToString())))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment