c# - 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 请求。
谢谢你
解决方案
如您在调用 API 的方法中提到的那样添加此代码
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
这对我有用。或者您可以升级到 .Net 框架 4.7,它会自动处理这个问题。
推荐阅读
- python - 使用 MSAL 访问 azure python sdk 中的 ObjectID?
- java - 在 Intellij Kotlin 应用程序中导入 java 类
- flutter - 颤振 - 如何在小部件上按计时器画线?
- python-3.x - 当我将 turicreate 可视化定位到“浏览器”时,为什么会出现“找不到资源”?
- java - MenuInflater 找不到菜单资源目录
- java - 有人可以向我解释以下用于递归的 Java 片段吗?
- php - Codeigniter 4实体属性转换为数组不自动序列化
- react-native - 默认 React Native 应用程序的打字稿注释错误?
- maven - java包名称和build.gradle中代码的依赖关系之间是否存在关系?
- laravel - Laravel 将图像嵌入电子邮件中,将图像显示为 Apple Mail 中的实际大小