Introduction
This article will guide you on how to post data to an HTTPS (i.e., secure connection) URL from a Windows application (.NET) by attaching a digital certificate from a certificate file and getting the response back. The code is written in C#.
Background
No special background knowledge is needed for this article. Any beginner or intermediate programmer will be able to understand this code.
Using the Code
You should have a valid certificate file which you will use to post data to a secure website/web service by attaching that certificate. Actually, by this, you are going to attach a public key to your HTTPS request. Another way is to go through all the certificates which are installed in your PC and get the right one from the store list and then attach the public key to your HTTPS request. You can use either method. But here, I am going to use the first method.
//
private void postFile()
{
HttpWebRequest req = null;
HttpWebResponse rsp = null;
X509Certificate cert = X509Certificate.CreateFromCertFile("d:\\cert\\abc.crt");
// Create a X509Certificat object from yor certificate.
// other way is to go through all the cerificates which are installed
// in your Pc and get the right one from the store list
string uri = "https://abc.com:2111/test.aspx";
// A url which is looking for the right public key with
// the incomming https request
String myfile = File.ReadAllText("C:\\somfile.xml");
req = (HttpWebRequest)System.Net.WebRequest.Create(uri);
String DataToPost = this.GetTextFromXMLFile(myfile);
String strSenderID = "123";
req.Method = "POST"; // Post method
req.ContentType = "application/octet-stream"; // content type
//You can also use ContentType = "text/xml";
req.Headers.Add("sender-id", strSenderID);
// Some Header information which you would like to send
// with the request
req.ContentLength = 1000;
req.KeepAlive = false;
req.UserAgent = null;
req.Timeout = 99999;

Toren ValonePosted Jun 23, 2010, 8:06 PM
I need to know how to change directory once logged on and how to get and send files via C#