首页 > 解决方案 > C# RestSharp 返回空字符串

问题描述

我的控制台应用程序和 webapi 应用程序中都有此代码

static void TestHitOtp()
    {
        try
        {
            string urlPOST = ConfigurationManager.AppSettings["OtpUrl"];
            string userid = ConfigurationManager.AppSettings["UserIdOtp"];
            string password = ConfigurationManager.AppSettings["PasswordOtp"];
            string phoneNumber = ConfigurationManager.AppSettings["PhoneNumberForTest"];
            string message = "message from test sms gateway from console app at " + DateTime.Now.ToString();
            var guid = Guid.NewGuid();
            var client = new RestClient(urlPOST);
            var request = new RestRequest(Method.POST);
            //ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            ParameterType type = ParameterType.GetOrPost;
            request.AddParameter("userid", userid, type);
            request.AddParameter("password", password, type);
            request.AddParameter("dept", "DeptName", type);
            request.AddParameter("original", "CompanyName", type);
            request.AddParameter("sendto", phoneNumber, type);
            request.AddParameter("messageid", guid.ToString(), type);
            request.AddParameter("message", message, type);
            var fullUrl = client.BuildUri(request);
            IRestResponse response = client.Execute(request);
            var content = response.Content;
            Console.WriteLine("Test OTP From Console");
            Console.WriteLine("User Id     : "+ userid);
            Console.WriteLine("Password    :" + password);
            Console.WriteLine("Dept        : DeptName");
            Console.WriteLine("Original    : CompanyName");
            Console.WriteLine("Message Id  : "+ guid.ToString());
            Console.WriteLine("message     : "+ message);
            Console.WriteLine("Result      : "+ content);
            Console.WriteLine("Press any key to close..");
            Console.ReadKey();
        }
        catch (Exception ex)
        {

            Console.WriteLine("Masuk Exception, Error Message = " + ex.Message);
            Console.WriteLine("Masuk Exception, Error InnerException Message = " + ex.InnerException.Message);
            Console.WriteLine("Press any key to close..");
            Console.ReadKey();
        }
    }

我很困惑。我有 2 个不同的环境生产服务器和临时服务器(服务器测试)。

但问题是这段代码在两个环境的控制台中运行良好。但在我的 webapi 中,这是主要的应用程序。此代码仅在登台服务器上运行。在生产服务器中,此命中的结果只是“空字符串”,我必须检查以确保这两个环境具有相同的配置,以便这两个环境可以访问我的短信网关服务器。是代理还是什么,如果它是代理要配置什么?web.config 还是什么?

我不知道我必须使用

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

如果我想点击 https 网址?

更新 :

我认为有一些被阻止的 restsharp 请求,所以 webapi 无法访问该 https url。但我不知道要检查什么。什么阻止了重新生成 http 请求。

谢谢你

标签: c#httpsrestsharpwebproxy

解决方案


如您在调用 API 的方法中提到的那样添加此代码

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

这对我有用。或者您可以升级到 .Net 框架 4.7,它会自动处理这个问题。


推荐阅读