【问题标题】:C# RestSharp Return Empty StringC# RestSharp 返回空字符串
【发布时间】:2018-12-04 03:54:30
【问题描述】:

我的控制台应用程序和 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 请求。

谢谢

【问题讨论】:

  • 删除 web api 应用程序中的控制台日志并尝试使用文件日志记录方法(如 log4net)。
  • console.writeline 仅存在于控制台应用程序中。但在 webapi 应用程序中,我没有放置任何 console.writeline。谢谢。

标签: c# https restsharp webproxy


【解决方案1】:

按照您在调用 API 的方法中提到的添加此代码

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多