通话宁静会返回错误请求。 RestSharp的成功。 HttpClient / HttpWebRequest失败

时间:2020-06-18 21:08:44

标签: c# api rest .net-core restsharp

好的!因此,我正在尝试拨打Polycom设备的电话。

为了隔离此代码,我使用了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令牌与RestSharp相同,并且主体是同一对象。

我真的无所适从,但是不幸的是我不能使用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();
    }

1 个答案:

答案 0 :(得分:0)

下面是提琴手的两个比较。

有问题的孩子在这里这行。

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 用户代理:RestSharp / 106.11.4.0 连接:保持活动 接受编码:gzip,放气 内容类型:application / json 内容长度:60 主机:192.168.50.76

{“数据”:{“目标”:“ 7164171568”,“行”:“ 1”,“类型”:“电话”}}

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 用户代理:FauxPaux 连接:保持活动 接受编码:放气 内容类型:application / json; 内容长度:60 主机:192.168.50.76

{“数据”:{“目标”:“ 7164171568”,“行”:“ 1”,“类型”:“电话”}}