I have a cURL command that requires a security certificate. I am trying to replace the call with a WebRequest in C#.
Here’s the curl command :
curl -d “uid=UUUUUU&password=PPPPPP&active=Y&type=I” https://www.aidap.naimes.faa.gov/aidap/XmlNotamServlet –key aidap_key.pem –cacert aidap_ca.pem –cert aidap_client.pem:start123 -k -O
Where I am stuck is with how to incorporate the –key, –cacert and –cert parameters. Currently I am setting WebRequest.PreAuthenticate=true and creating NetworkCredential with the name and password of the security certificate in my certificate store (what I assume is in the –key, –cacert, etc).
I am getting a 403 forbidden exception when I run the code below.
Any ideas?
Here’s the code I’m using:
public void RetrieveNotams()
{
string myURL = "https://www.aidap.naimes.faa.gov/aidap/XmlNotamServlet";
HttpWebRequest req;
NetworkCredential myCred = new NetworkCredential("AAAAA", "BBBBB");
req = (HttpWebRequest)WebRequest.Create(myURL);
req.PreAuthenticate = true;
req.Credentials = myCred;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string postData = "uid=UUUUUU&password=PPPPPP&active=Y&type=I";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
req.ContentLength = byteArray.Length;
using (Stream dataStream = req.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
req.GetResponse();
}
I figured it out with the help of
http://www.codeproject.com/Articles/28395/Attaching-a-digital-certificate-public-key-to-an-H
I grab the certificate from the certificate store, and add it to the request’s certificate collection. And took out the PreAuthenticate code.
Here’s the updated code: