0
目前,我已经在webapi中创建了一个通过上传x509证书发送请求的方法。应用程序接收请求c#
protected void AttachClientCertificate(HttpWebRequest request, string userName, string certPath)
{
// The path to the certificate.
string Certificate = @"C:\Users\kuvinodh\Desktop\New folder\sfdfdf.pfx";
X509Certificate certificate = new X509Certificate();
certificate.Import(Certificate, "password", X509KeyStorageFlags.PersistKeySet);
request.ClientCertificates.Add(certificate);
}
[SwaggerResponse(HttpStatusCode.Forbidden)]
[SwaggerResponse(HttpStatusCode.Accepted)]
[SwaggerResponse(HttpStatusCode.Unauthorized)]
[SwaggerResponse(HttpStatusCode.NotFound)]
[SwaggerResponse(HttpStatusCode.ServiceUnavailable)]
[System.Web.Http.Route("SendReuest")]
[System.Web.Http.HttpPost]
public string GetResponse(string requestUri, string postData, string userName, string certPath)
{
// Create the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = "POST";
request.ContentType = "application/dfdfd";
request.ContentLength = postData.Length;
// Attach the client certificate if https and certPath specified.
if (new Uri(requestUri).Scheme == Uri.UriSchemeHttps && certPath != "")
{
AttachClientCertificate(request, userName, certPath);
}
// Write data to request
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
try
{
requestWriter.Write(postData);
}
finally
{
requestWriter.Close();
}
// Send to the remote server and wait for the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Read the response
string responseString;
StreamReader responseReader = new StreamReader(response.GetResponseStream());
try
{
responseString = responseReader.ReadToEnd();
}
finally
{
responseReader.Close();
}
// Return response
return responseString;
}
现在我需要编写和应用程序接收到这个请求由API发送和验证证书,请你让我知道如何这是一定要做?我非常喜欢这个。
您是否正在编写这段代码会调用的Web服务,即通过'requestUri'参数传入的URL? – gmiley
正如我所说我对此完全陌生,只需要一个将听取请求并接收它的应用程序。如果Webservice这样做,那么请让我知道这是必须完成的? – Vinodh