我有一个执行此操作的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#中复制该步骤。