2016-11-15 74 views
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发送和验证证书,请你让我知道如何这是一定要做?我非常喜欢这个。

+1

您是否正在编写这段代码会调用的Web服务,即通过'requestUri'参数传入的URL? – gmiley

+0

正如我所说我对此完全陌生,只需要一个将听取请求并接收它的应用程序。如果Webservice这样做,那么请让我知道这是必须完成的? – Vinodh

回答

0

您可能希望使用Web服务作为您的接收器。要解释如何创建Web服务将是不切实际的,因为通过在Google上快速搜索C# Web ServiceC# RESTful Web Service example可以获得大量示例。

这里有两个结果,让你开始:

如果你通过一个或两个示例项目的工作,你遇到了问题,你可以不知道,回来发布一个新问题。事实上,这个问题有点含糊,所以这可能与你将要得到的具体答案有关。