Restful 调用 returns 错误请求。在 RestSharp 中取得成功。失败 HttpClient/HttpWebRequest
Restful call returns Bad Request. Success in RestSharp. Failed with HttpClient/HttpWebRequest
好的!所以我有这个 Restful 呼叫,我正试图对 Polycom 设备进行呼叫。
为了在这段代码中进行隔离,我使用了 RestSharp,它工作正常。此外,删除凭据对象或更改它会使它退回未授权,因此我相信授权是可以的。我认为 body/parameters 不正确。
//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 令牌与 RestSharp 相同,正文是相同的对象。
我真的不知道我能做什么,不幸的是我不能使用 restsharp 我需要使用 Net Core 中可用的东西。
我在代码中有几个不同的实现,如下所示。我尝试使用 HttpClient 并尝试使用 HttpWebRequest。
我的问题是...我可能有什么错误阻止了它成为正确的请求。
这里的 link 是 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的两个比较。
问题child是这里的这一行。
request.ContentType = "application/json; charset=utf-8";
修复??
request.ContentType = "application/json";
以下是导致发现的提琴手结果。
POST https://192.168.50.76/api/v1/callctrl/dial HTTP/1.1
授权:基本UG9seWNvbTp3TUh0bDNKdA==
接受:application/json、text/json、text/x-json、text/javascript、application/xml、text/xml
User-Agent: RestSharp/106.11.4.0
连接:Keep-Alive
Accept-Encoding: gzip, deflate
Content-Type: application/json
Content-Length: 60
主机:192.168.50.76
{"data":{"Dest": "7164171568", "Line": "1","Type": "TEL" }}
POST https://192.168.50.76/api/v1/callctrl/dial HTTP/1.1
授权:基本UG9seWNvbTp3TUh0bDNKdA==
接受:application/json、text/json、text/x-json、text/javascript、application/xml、text/xml
User-Agent:人造假
连接:Keep-Alive
Accept-Encoding: 放气
Content-Type: application/json;
Content-Length: 60
主机:192.168.50.76
{"data":{"Dest": "7164171568", "Line": "1","Type": "TEL" }}
好的!所以我有这个 Restful 呼叫,我正试图对 Polycom 设备进行呼叫。
为了在这段代码中进行隔离,我使用了 RestSharp,它工作正常。此外,删除凭据对象或更改它会使它退回未授权,因此我相信授权是可以的。我认为 body/parameters 不正确。
//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 令牌与 RestSharp 相同,正文是相同的对象。
我真的不知道我能做什么,不幸的是我不能使用 restsharp 我需要使用 Net Core 中可用的东西。
我在代码中有几个不同的实现,如下所示。我尝试使用 HttpClient 并尝试使用 HttpWebRequest。
我的问题是...我可能有什么错误阻止了它成为正确的请求。
这里的 link 是 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的两个比较。
问题child是这里的这一行。
request.ContentType = "application/json; charset=utf-8";
修复??
request.ContentType = "application/json";
以下是导致发现的提琴手结果。
POST https://192.168.50.76/api/v1/callctrl/dial HTTP/1.1 授权:基本UG9seWNvbTp3TUh0bDNKdA== 接受:application/json、text/json、text/x-json、text/javascript、application/xml、text/xml User-Agent: RestSharp/106.11.4.0 连接:Keep-Alive Accept-Encoding: gzip, deflate Content-Type: application/json Content-Length: 60 主机:192.168.50.76
{"data":{"Dest": "7164171568", "Line": "1","Type": "TEL" }}
POST https://192.168.50.76/api/v1/callctrl/dial HTTP/1.1 授权:基本UG9seWNvbTp3TUh0bDNKdA== 接受:application/json、text/json、text/x-json、text/javascript、application/xml、text/xml User-Agent:人造假 连接:Keep-Alive Accept-Encoding: 放气 Content-Type: application/json; Content-Length: 60 主机:192.168.50.76
{"data":{"Dest": "7164171568", "Line": "1","Type": "TEL" }}