使用C#开发的https REST API [英] Consume https REST API developed in C#

查看:736
本文介绍了使用C#开发的https REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我们已经将我们的应用程序与一个第三方集成在一起。我们正在使用他们在C#应用程序中用Java开发的Rest API。



他们已经禁用了除TSL1.2之外的服务器中的所有协议(即SSL2,TSL1.0) ,TSL1.1)



当我们调用Rest API时,我们收到错误。请在下面找到我的代码



我尝试过:



Hi All,

We have done integrated our application with one third party. We are consuming their Rest API developed in Java from C# application.

They have disabled all the protocols in the server other than TSL1.2(i.e. SSL2, TSL1.0, TSL1.1)

When we call the Rest API we are getting the error. Please find below my code

What I have tried:

string response;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls|SecurityProtocolType.Ssl3;
        ServicePointManager.ServerCertificateValidationCallback = delegate(object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
        WebClient client = new WebClient();
        string credentials;
      
        credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("gujarat_ws_client" + ":" + "guj@r@t_C|ient"));
        client.Headers.Add("Authorization", "Basic Z3VqYXJhdF93c19jbGllbnQ6Z3VqQHJAdF9DfGllbnQ=");                                             
        try
        {
            
            var strEaushadhiURI = "https://xxx.com/";

            System.Net.ServicePointManager.ServerCertificateValidationCallback = (object se, System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslerror) => true;
            response = client.DownloadString(new Uri(strEaushadhiURI + "/AMH/" + txtVoucherNumber.Text.Trim()));
            System.Net.ServicePointManager.ServerCertificateValidationCallback = null;
            TextBox1.Text = response;
        }
        catch (Exception ex)
        {
            TextBox1.Text = ex.Message;
        }





但我收到错误:



But I am getting error :

The remote server returned an error: (401) Unauthorized.

推荐答案

证书是为了确保服务器有效/安全;它与您访问所述服务器的权限无关。



您根本就没有正确登录;暗示访问需要帐户和密码或安全令牌。
The "certificate" is to insure the server is "valid / secure"; it has nothing to do with your "authority" to gain access to said server.

You are simply not "signing in" properly; implying an "account and password" or security token is required for access.


我遇到了同样的问题,这是因为我的请求标题。

我会为您提供示例代码:



httpWReq.UserAgent =Apache-HttpClient / 4.2.1;

httpWReq.Accept = text / html,image / gif,image / jpeg,*; q = .2,* / *; q = .2;

httpWReq.ContentType =text / plain;

httpWReq.Headers.Add(授权,基本+ Convert.ToBase64String(authBytes));

httpWReq.Headers.Add(Signature,CanonicalReq);

httpWReq.Headers.Add(SignedHeaders,host);

/ * httpWReq.Headers.Add(content-Type,text / plain)* /

//用户代理

httpWReq.ProtocolVersion = HttpVersion.Version11;

httpWReq.Method =GET;





一切顺利。
I had the same problem and it was because of my request headers.
I will put a sample code for you :

httpWReq.UserAgent = "Apache-HttpClient/4.2.1";
httpWReq.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
httpWReq.ContentType = "text/plain";
httpWReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(authBytes));
httpWReq.Headers.Add("Signature", CanonicalReq);
httpWReq.Headers.Add("SignedHeaders", "host");
/* httpWReq.Headers.Add("content-Type", "text/plain")*/
//user agent
httpWReq.ProtocolVersion = HttpVersion.Version11;
httpWReq.Method = "GET";


All the best.


这篇关于使用C#开发的https REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆