我正在为VMware工作站15 REST API的控制应用程序(c#WPF)实习,以:
我当前正在使用.NET HTTPClient向VMServer发送指令,到目前为止,我已经能够从任何可能的GET命令中获取所有信息,但是我无法弄清楚如何避免从中获取错误406。尝试使用PUT命令打开或关闭服务器上的任何VM。我尝试将以下内容用作接受标头和媒体/内容类型的不同组合:
执行订单的代码由Button Click事件触发:
//Needs a VMID, valid IP and valid Port, Password and User will be handled aswell, but aren't requiered to start the request
if (tb_VMID.Text.Length > 0 && isIPVailid && isPortValid)
{
string Link = "https://" + tb_IP.Text + ":" + tb_Port.Text + "/api/vms/" + tb_VMID.Text + "/power";
//here starts the preperation of the httpclient info
using (HttpClientHandler _handler = new HttpClientHandler())//Handler for credentials
{
_handler.Credentials = new NetworkCredential(tb_Username.Text, tb_Pass.SecurePassword);
using (HttpClient _httpClient = new HttpClient(_handler))//Client with Credentials
{
//Header handling (Note that the passowrd is encrypded in base 64 and needs to be that way)
_httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + CreateBaseCode());
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.vmware.vmw.rest-v1+json"));
//Request preperation
using (HttpContent _content = new StringContent("on", Encoding.UTF8, "application/vnd.vmware.vmw.rest-v1+json"))
{
//now here starts the PUT request, it's async but I already have 2 working async methods to GET info, would be wierd if that would be the problem, especialy since i tried the same without async.
var data = _httpClient.PutAsync(Link, _content);
tb_Response.Text = data.Result.ToString();
}
}
}
}
结果如下:
StatusCode: 406, ReasonPhrase: 'Not Acceptable', Version: 1.1, Content:
System.Net.Http.StreamContent, Headers:
{
Access-Control-Allow-Origin: *
Connection: close
Cache-Control: no-cache
Date: Fri, 12 Apr 2019 12:42:04 GMT
Content-Length: 58
Content-Type: application/vnd.vmware.vmw.rest-v1+json
}
我的问题是,我可以解决此错误,还是REST或.NET Framework有问题?