Add ClientCertificates #4

Merged
merged 2 commits into from Mar 30, 2012
View
9 src/EasyHttp/Http/HttpClient.cs
@@ -58,6 +58,7 @@
using System;
using System.Collections.Generic;
+using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using EasyHttp.Codecs;
using EasyHttp.Configuration;
@@ -211,7 +212,13 @@ HttpResponse ProcessRequest()
return Response;
}
-
+ public void AddClientCertificates(X509CertificateCollection certificates)
+ {
+ if(certificates == null || certificates.Count == 0)
+ return;
+
+ Request.ClientCertificates.AddRange(certificates);
+ }
bool IsHttpError()
{
View
15 src/EasyHttp/Http/HttpRequest.cs
@@ -79,6 +79,7 @@ public class HttpRequest
public string AcceptEncoding { get; set; }
public string AcceptLanguage { get; set; }
public bool KeepAlive { get; set; }
+ public X509CertificateCollection ClientCertificates { get; set; }
public string ContentLength { get; private set; }
public string ContentType { get; set; }
public string ContentEncoding { get; set; }
@@ -124,7 +125,9 @@ public bool ForceBasicAuth
public HttpRequest(IEncoder encoder)
{
RawHeaders = new Dictionary<string, object>();
-
+
+ ClientCertificates = new X509CertificateCollection();
+
UserAgent = String.Format("EasyHttp HttpClient v{0}",
Assembly.GetAssembly(typeof(HttpClient)).GetName().Version);
@@ -361,8 +364,18 @@ public HttpWebRequest PrepareRequest()
return httpWebRequest;
}
+ private void SetupClientCertificates()
+ {
+ if (ClientCertificates == null || ClientCertificates.Count == 0)
+ return;
+
+ httpWebRequest.ClientCertificates.AddRange(ClientCertificates);
+ }
+
void SetupAuthentication()
{
+ SetupClientCertificates();
+
if (_forceBasicAuth)
{
string authInfo = _username + ":" + _password;