[英]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);
我試圖刪除帶有郵遞員令牌,緩存控制的行,但始終相同,沒有錯誤,沒有響應。 (在響應中,我應該獲得訪問令牌)
但是我也認為您在將主體作為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;
通話之前。
您只能嘗試進行測試:
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";
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.