|
发表于 2017-11-15 14:34:42
|
显示全部楼层
我的代码如下
public static void TestUrl()
{
string Url = "https://xxxx:8743/iocm/app/sec/v1.1.0/login";
string CertPath = "outgoing.CertwithKey.pkcs12";
string CertPwd = "IoM@1234";
//string Cert2Path = "ca.jks";
//string Cert2Pwd = "Huawei@123";
string Content = "appId=xxxx&secret=xxxx";
HttpResult result = new HttpResult();
HttpWebRequest webReqst = (HttpWebRequest)WebRequest.Create(Url);
X509Certificate2 cert = CreateCert(CertPath, CertPwd);
webReqst.ClientCertificates.Add(cert);
webReqst.Method = "POST";
webReqst.ContentType = "application/x-www-form-urlencoded";
webReqst.KeepAlive = true;
try
{
if (!string.IsNullOrWhiteSpace(Content))
{
byte[] data = Encoding.UTF8.GetBytes(Content);
webReqst.ContentLength = data.Length;
Stream stream = webReqst.GetRequestStream();
stream.Write(data, 0, data.Length);
}
WebResponse respon = webReqst.GetResponse();
using (HttpWebResponse webResponse = (HttpWebResponse)webReqst.GetResponse())
{
result.HttpStatusCode = webResponse.StatusCode;
StreamReader reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
result.Html = reader.ReadToEnd();
webResponse.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
} |
|