1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| byte[] aryData = Encoding.UTF8.GetBytes(string.Empty);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("URL");
//// 設定私有憑證 req.ClientCertificates.Add(new X509Certificate(HttpRuntime.AppDomainAppPath + @"\yourCertificates.p12", "yourpassword")); X509Store certStore = new X509Store("My", StoreLocation.LocalMachine); certStore.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2 cert = certStore.Certificates[0]; certStore.Close(); req.ClientCertificates.Add(cert);
req.Method = "POST"; req.ContentType = "application/json"; req.ContentLength = aryData.Length; req.Accept = "application/json";
using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(aryData, 0, aryData.Length); }
string strResult = string.Empty;
using (WebResponse res = req.GetResponse()) { using (Stream resStream = res.GetResponseStream()) { using (StreamReader objSR = new StreamReader(resStream)) { strResult = objSR.ReadToEnd(); } } }
return strResult;
|