你如何从ac#客户端发送补丁请求?
我有一个PowerShell脚本,它执行此操作:
$uri = "$($tfsUri)/$($teamproject)/_apis/build/builds/$($buildID)?api-version=2.0" $data = @{keepForever = $keepForever} | ConvertTo-Json $response = $webclient.UploadString($uri,"PATCH", $data)
我正在尝试使用Webclient在C#中重写它。
WebClient client = new WebClient(); client.Encoding = System.Text.Encoding.UTF8; string reply = client.UploadString(url, "keepForever = true"); Console.WriteLine(reply);
但我得到:远程服务器返回错误:(401)未经授权。
这是TFS 2015 VNext,如果有帮助的话。
您在调用UploadString时缺少METHOD。
string reply = client.UploadString(url, "keepForever = true");
应该:
string reply = client.UploadString(url, "PATCH", "keepForever = true");
401是未经授权的,因此,如果您在Powershell中有登录或加入会话的步骤,则需要在C#中复制该步骤。
要发送PATCH请求,您可以使用WebClient.UploadData
。
string data = "keepForever = true"; WebClient client = new WebClient(); client.Encoding = System.Text.Encoding.UTF8; string reply = client.UploadData(url, "PATCH", System.Text.Encoding.UTF8.GetBytes(data)); Console.WriteLine(reply);