Json Post 在 Postman 中有效,但在 C# 中无效
Json Post works in Postman but not in C#
当我在 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 的行,但总是一样,没有错误,没有响应。 (在响应中我应该获得访问令牌)
您可以尝试这个仅用于测试:
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";
};
但我也认为您可能遇到问题,因为您将正文作为 JSON 传递,RestSharp 将再次尝试将其序列化为 JSON。试试这个。
创建一个 class 来保存您的参数
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;
通话前。
当我在 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 的行,但总是一样,没有错误,没有响应。 (在响应中我应该获得访问令牌)
您可以尝试这个仅用于测试:
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";
};
但我也认为您可能遇到问题,因为您将正文作为 JSON 传递,RestSharp 将再次尝试将其序列化为 JSON。试试这个。
创建一个 class 来保存您的参数
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;
通话前。