如何通过HTTPS请求验证证书

时间:2015-06-11 10:45:53

标签: c# asp.net

我需要通过POST方法调用URL。但是我在通话期间收到证书问题。这是我的代码,

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public string CurrencyConverter(string IssueId,string FromCurrency,string ToCurrency)
    {
        string url;
        url = "http://www.webservicex.net/currencyconvertor.asmx/ConversionRate?FromCurrency=" + FromCurrency + "&ToCurrency=" + ToCurrency;
        HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
        webReq.Method = "GET";
        HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse();
        //I don't use the response for anything right now. But I might log the response answer later on.   
        Stream answer = webResponse.GetResponseStream();
        StreamReader _recivedAnswer = new StreamReader(answer);
       string content=_recivedAnswer.ReadToEnd();
        XmlDocument xd = new XmlDocument();
        xd.LoadXml(content);
        content=xd.LastChild.InnerText;


        url = "https://jira-mysql:8443/rest/api/2/issue/CPS-306";
        WebRequest myReq = WebRequest.Create(url);

        string username = "nmorshedi@aversan.com";
        string password = "Aversan1";
        string data = "{\"fields\":{\"Description\":\"" + content + "\"}}";
        //ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

        // You must change the URL to point to your Web server.
        X509Certificate Cert = X509Certificate.CreateFromCertFile("C:\\jira.cer");
        System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);
        CredentialCache mycache = new CredentialCache();
        mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
        Request.Credentials = mycache;
        Request.ClientCertificates.Add(Cert);
        Request.UserAgent = "Client Cert Sample";
        Request.Method = "POST";
        Request.ContentLength = data.Length;
        Request.ContentType = "application/json; charset=UTF-8";
        Request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + "&" + password)));

        HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
        Stream receiveStream = Response.GetResponseStream();
        StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
        content = reader.ReadToEnd();
        return content;
    } // callback used to validate the certificate in an SSL conversation
    //Implement the ICertificatePolicy interface.
    public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
    {
        public TrustAllCertificatePolicy()
        { }
        public bool CheckValidationResult(ServicePoint sp,
           System.Security.Cryptography.X509Certificates.
            X509Certificate cert, WebRequest req, int problem)
        {

            return true;
        }
    }
}

继续加载。任何人都可以帮助我吗?

0 个答案:

没有答案