简体   繁体   English

无法在c#中使用Zoopla webservice

[英]Not able to consume Zoopla webservice in c#

I want to consume a web service ( https://realtime-listings-api.webservices.zpg.co.uk/sandbox/v1/listing/list ) and as per there documentation ( https://realtime-listings.webservices.zpg.co.uk/docs/latest/documentation.html ) I have to send .crt and .pem file for authentication. 我想使用一个Web服务( https://realtime-listings-api.webservices.zpg.co.uk/sandbox/v1/listing/list )并根据那里的文档( https://realtime-listings.webservices。 zpg.co.uk/docs/latest/documentation.html )我必须发送.crt和.pem文件进行身份验证。 I am able to load .crt file but for .pem I am getting error that Cannot find the requested object . 我能够加载.crt文件,但对于.pem我收到错误, Cannot find the requested object I have tried different method to load PEM file. 我尝试了不同的方法来加载PEM文件。

I have followed following threads but still not able to load X509Certificate from .pem file. 我已遵循以下线程但仍无法从.pem文件加载X509Certificate

My code is as below 我的代码如下

var webAddr = "https://realtime-listings-api.webservices.zpg.co.uk/sandbox/v1/listing/list";
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                    httpWebRequest.ContentType = "application/json; profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.1/schemas/listing/list.json";
                    httpWebRequest.Method = "POST";

httpWebRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile(@"E:\ProcessZooplaData\zpg_realtime_listings_14810206-20261204.crt"));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Now till here everything is Okay now If I try to load .pem file then I am getting error 现在到这里一切都好了如果我尝试加载.pem文件然后我收到错误

var pem = System.IO.File.ReadAllText(@"E:\\ProcessZooplaData\\private.pem");
byte[] certBuffer = GetBytesFromPEM(pem, "RSA PRIVATE KEY");
                    var certificate = new X509Certificate(certBuffer);
                    httpWebRequest.ClientCertificates.Add(certificate);



byte[] GetBytesFromPEM(string pemString, string section)
        {
            var header = String.Format("-----BEGIN {0}-----", section);
            var footer = String.Format("-----END {0}-----", section);

            var start = pemString.IndexOf(header, StringComparison.Ordinal);
            if (start < 0)
                return null;

            start += header.Length;
            var end = pemString.IndexOf(footer, start, StringComparison.Ordinal) - start;

            if (end < 0)
                return null;

            return Convert.FromBase64String(pemString.Substring(start, end));
        }

I am getting error here that Cannot find the requested object . 我在这里得到错误, Cannot find the requested object Rest of code is as below 其余代码如下

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {
                        string json = "{\"branch_reference\":\"test\"}";

                        streamWriter.Write(json);
                        streamWriter.Flush();
                    }

                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        //return result;
                    }

I have tried following threads for reference how to get private key from PEM file? 我试过跟踪线程以获取如何从PEM文件获取私钥的参考

http://pages.infinit.net/ctech/20040812-0816.html http://pages.infinit.net/ctech/20040812-0816.html

If your certificate has already been loaded into your cert store, then you can do the following: 如果您的证书已经加载到您的证书库中,那么您可以执行以下操作:

var requestHandler = new WebRequestHandler();
var store = new X509Store("My", StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certificates = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, certificateName, true);
if (certificates.Count > 0)
    requestHandler.ClientCertificates.Add(certificates[0]);
else
    throw new Exception(string.Format("Can't find certificate {0}", certificateName));
using (var client = new HttpClient(requestHandler))
{
    do work!
}

which should add the cert from the store to the connection. 应该将商店中的证书添加到连接中。

Then you'll need to open up the cert store in the Certificates (run mmc, add Certificates to the console for the local computer store), browse to your certificate, right click it and select All Tasks > Manage Private Keys and grant the user account your application will run under "read" access to the cert's private key and you won't need to open any files to do this. 然后,您需要在证书中打开证书库(运行mmc,将证书添加到本地计算机商店的控制台),浏览到您的证书,右键单击它并选择所有任务>管理私钥并授予用户帐户您的应用程序将在“读取”访问证书的私钥下运行,您不需要打开任何文件来执行此操作。

Of course, if you're doing this on a shared hosting environment where you don't get access to the certs or can't install them into the cert store, that's a different problem. 当然,如果您在无法访问证书的共享托管环境中执行此操作,或者无法将它们安装到证书库中,则这是一个不同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM