Using HttpWebRequest with X509Certificate2

Using HttpWebRequest with X509Certificate2 

public string HttpsPostData(string url, string xmlReq)
        {
            string str = null;
            try
            {
                int num;
                string cerFile = String.Concat(Directory.GetCurrentDirectory(),
 "/Vendors/Cer/Hogaming.cer");
                var cert = new X509Certificate2(cerFile);               
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + xmlReq);
                request.ClientCertificates.Add(cert);
                request.UserAgent = "Client Cert Sample";
                request.Method = "GET";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), 
Encoding.Default);
                char[] buffer = new char[0x400];
                do
                {
                    num = reader.Read(buffer, 0, 0x400);
                    if (num != 0)
                    {
                        str = new string(buffer);
                    }
                }
                while (num > 0);
            }
            catch (Exception exception)
            {
                str = null;
                logger.WriteLog("HttpsPostData", "request: " + url + xmlReq 
+ "; response" + str, exception.Message);
            }
            return str;
        }

Reactions

Post a Comment

0 Comments