当我使用邮递员并输入不完整的 MD 哈希值时,它会正确显示 MD 哈希值有问题的错误:
但是当我尝试使用 Restsharp 在 C# 中执行相同操作时,我总是收到通用错误消息“请求失败,状态代码为 BadRequest”。 如果我输入错误的数据库名称或错误的密码或用户名,我只会收到一条错误消息。与此同时,我在邮递员中收到了确切的消息。
C#代码:
using RestSharp;
var client = new RestClient("URL");
var request = new RestRequest();
//request.AddBody("Grant_type=password&Scope=Innovator&Client_id=IOMApp&Username=vipul&Password=21c603dbf852f78063f98674640ab339&Database=DATABASENAME");
//request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
//request.AddParameter("Grant_type", "password");
//request.AddParameter("Scope", "Innovator");
//request.AddParameter("Client_id", "IOMApp");
//request.AddParameter("Username", "vipul");
//request.AddParameter("Password", "21c603dbf852f78063f98674640ab33");
//request.AddParameter("Database", "DATABASENAME");
request.AddObject (new
{
Grant_type = "password",
Scope = "Innovator",
Client_id = "IOMApp",
Username = "vipul",
Password = "21c603dbf852f78063f98674640ab33",
Database = "DATABASENAME"
});
var response = client.Post(request);
当我使用错误的 MD Hash 时的 C# 输出
System.Net.Http.HttpRequestException
HResult=0x80131500
Message=Request failed with status code BadRequest
Source=RestSharp
StackTrace:
at RestSharp.RestClientExtensions.<PostAsync>d__18.MoveNext()
at RestSharp.AsyncHelpers.<>c__DisplayClass1_0`1.<<RunSync>b__0>d.MoveNext()
at RestSharp.AsyncHelpers.CustomSynchronizationContext.<<Run>g__PostCallback|7_0>d.MoveNext()
at RestSharp.AsyncHelpers.CustomSynchronizationContext.Run()
at RestSharp.AsyncHelpers.RunSync(Func`1 task)
at RestSharp.AsyncHelpers.RunSync[T](Func`1 task)
at Program.<Main>$(String[] args) in C:\Users\201031703\source\repos\basic api\basic api\Program.cs:line 30
请求失败,状态代码为 BadRequest
有人可以帮我解决这个问题吗?
我用过
var response = await client.PostAsync(uri, content);
var result = await response.Content.ReadAsStringAsync();
这解决了我的问题,我没有抛出异常,而是在变量中得到了正确的响应。