【发布时间】:2020-06-19 06:31:53
【问题描述】:
好的!因此,我尝试向 Polycom 设备拨打这个 Restful 电话。
为了在这段代码中进行隔离,我使用了 RestSharp,它工作正常。还删除凭据对象或更改它会使其反击未经授权,因此我相信授权很好。我认为它的主体/参数不正确。
//THIS SECTION WORKS
//RestRequest restRequest = new RestRequest();
//restRequest.AddJsonBody(body);
//RestClient restClient = new RestClient(targetUrl);
//restClient.RemoteCertificateValidationCallback = (sPoint, cert, wRequest, certProb) => true;
//restClient.Authenticator = new HttpBasicAuthenticator(username, deskPhone.MACPassword);
//IRestResponse restsharpResponse = await restClient.ExecutePostAsync(restRequest).ConfigureAwait(false);
//END SECTION
我的问题是什么.. 我不知道为什么会收到错误 400 Bad Request。当我查看通话中的内容时,一切似乎都正确对齐。 Auth token 和 RestSharp 一样,body 是同一个对象。
我真的不知所措,不幸的是我不能使用restsharp,我需要使用Net Core中可用的东西。
我在代码中有几个不同的实现,如下所示。我尝试使用 HttpClient 并尝试使用 HttpWebRequest。
我的问题是......我有什么错误可能会阻止这是一个正确的请求。
此处的此链接指向 API 文档,我似乎也正确地对齐了该文档。 https://support.polycom.com/content/dam/polycom-support/products/voice/polycom-uc/other-documents/en/2018/ucsoftware-restapi.pdf
[HttpPost]
[Route("[action]")]
public async Task PhoneNumberStuff(long userId)
{
DeskPhone deskPhone = DbContext.DeskPhones
.Include(x=>x.DeskPhoneUser)
.FirstOrDefault(x => x.DeskPhoneUserId == 11546);
string targetUrl = $"https://{deskPhone.IPv4Address}/api/v1/callctrl/dial";
string certLcation = @"C:\Users\AlexRoundy\Desktop\Polycom.cer";
string username = "Polycom";
SecureString password = new NetworkCredential("", deskPhone.MACPassword).SecurePassword;
String encoded = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + deskPhone.MACPassword));
//$cred = New - Object System.Management.Automation.PSCredential($username,$password)
NetworkCredential credential = new NetworkCredential(username, password);
string body = @"{""data"":{""Dest"": ""7168675309"", ""Line"": ""1"",""Type"": ""TEL"" } }";
//THIS SECTION WORKS
//RestRequest restRequest = new RestRequest();
//restRequest.AddJsonBody(body);
//RestClient restClient = new RestClient(targetUrl);
//restClient.RemoteCertificateValidationCallback = (sPoint, cert, wRequest, certProb) => true;
//restClient.Authenticator = new HttpBasicAuthenticator(username, deskPhone.MACPassword);
//IRestResponse restsharpResponse = await restClient.ExecutePostAsync(restRequest).ConfigureAwait(false);
//END SECTION
HttpClientHandler handler = new HttpClientHandler();
//handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
handler.ServerCertificateCustomValidationCallback = (sPoint, cert, wRequest, certProb) => true;
//handler.CheckCertificateRevocationList = false;
handler.Credentials = credential;
//handler.ClientCertificateOptions = ClientCertificateOption.Manual;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUrl);
request.ServerCertificateValidationCallback = (sPoint, cert, wRequest, certProb) => true;
request.ContentType = "application/json; charset=utf-8";
request.Method = "POST";
request.Headers.Add("Authorization", "Basic " + encoded);
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
request.Accept = "application/json";
using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(body);
streamWriter.Flush();
}
HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
string responseText;
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
HttpClient httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
StringContent temp = new StringContent(body, Encoding.UTF8, "application/json");
HttpRequestMessage httpMessage = new HttpRequestMessage(HttpMethod.Post, targetUrl);
httpMessage.Content = temp;
HttpResponseMessage testA = await httpClient.SendAsync(httpMessage).ConfigureAwait(false);
HttpResponseMessage stuff = await httpClient.PostAsync(targetUrl, temp).ConfigureAwait(false);
handler.Dispose();
httpMessage.Dispose();
httpClient.Dispose();
}
【问题讨论】:
-
首先要做的是在 fiddler 中比较它们。
-
您确定在 restsharp 代码中没有收到 400 响应吗?你如何验证这一点?
-
将军!这实际上是我在发布后立即做的......我即将分享。
-
Chetan 我知道这不是 400 响应,因为在调试器中我得到了 200,还因为端点完成了它的工作并正确地拨了我。
标签: c# api rest .net-core restsharp