当我在Postman中尝试Post请求时,它给出了正确的响应,没有错误。当我使用Postman生成的Restsharp代码时,响应始终为空,没有错误。
var client = new RestClient("https://myurl/api/authenticate/authenticate");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "00497e4f-f58f-677d-f98a-bb972032c2eb");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\n\t\"applicationKey\" : \"MYAPPLICATIONKEY\",\n\t\"userSecret\" : \"MYUSERSECRET\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
我试图用postman-token,cache-control删除这些行但总是一样没有错误没有响应。 (在响应中我应该获得访问令牌)
答案 0 :(得分:2)
但我也认为你可能有一个问题,因为你将身体作为JSON传递,而RestSharp将尝试再次将其序列化为JSON。试试这个。
创建一个类来保存参数
public class Body
{
public string applicationKey { get; set; }
public string userSecret { get; set; }
}
并将其作为参数内容传递
var client = new RestClient("https://myurl");
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.Resource = "api/authenticate/authenticate";
var body = new Body();
body.applicationKey = "MYAPPLICATIONKEY";
body.userSecret = "MYUSERSECRET";
request.AddBody(body);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
原来是TLS版本问题。
通过添加
修复ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
在电话会议之前。
答案 1 :(得分:0)
您只能尝试测试:
public static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
{
return true;
};
//...
//Your Rest Call
}
对于内部使用,您可以执行以下操作:
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) =>
{
return cert.GetCertHashString() == "server certificate hash";
};