C#RestSharp返回空字符串

时间:2018-12-04 03:54:31

标签: c# https restsharp webproxy

我的控制台应用程序和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中,这是主要应用程序。此代码仅在 登台服务器。在生产服务器中,此命中的结果只是“空字符串”,我必须检查以确保这两个环境具有相同的配置,以便这两个环境可以命中我的sms网关服务器。是代理还是什么(如果是代理)要配置什么? web.config还是什么?

我不知道我必须使用

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

如果我想点击https url吗?

更新:

我认为存在一些阻止restsharp请求的内容,因此webapi无法访问该https网址。但我不知道要检查什么。什么阻止了restharp http请求。

谢谢

1 个答案:

答案 0 :(得分:0)

按照调用API的方法中所述添加此代码

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

这对我有用。或者,您可以升级到将自动处理此问题的.Net Framework 4.7。