简体   繁体   English

您如何从ac#客户端发送补丁请求?

[英]How do you send a patch request from a c# client?

I have a powershell script that does this : 我有一个执行此操作的powershell脚本:

    $uri = "$($tfsUri)/$($teamproject)/_apis/build/builds/$($buildID)?api-version=2.0"
    $data = @{keepForever = $keepForever} | ConvertTo-Json
    $response = $webclient.UploadString($uri,"PATCH", $data) 

I'm trying to rewrite this in C#, using a Webclient. 我正在尝试使用Webclient在C#中重写它。

WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;
string reply = client.UploadString(url, "keepForever = true");
Console.WriteLine(reply);

But I get : The remote server returned an error: (401) Unauthorized. 但我得到:远程服务器返回错误:(401)未经授权。

This is TFS 2015 VNext, if that helps. 这是TFS 2015 VNext,如果有帮助的话。

You are missing the METHOD in your call to UploadString . 您在对UploadString的调用中缺少METHOD。

string reply = client.UploadString(url, "keepForever = true");

should be: 应该:

string reply = client.UploadString(url, "PATCH", "keepForever = true");

A 401 is unauthorised, so also see if there is a step before in your Powershell where you are logging in or joining a session, you would need to replicate that in your C#. 401是未经授权的,因此还要查看在Powershell中是否有登录或加入会话的步骤,您需要在C#中复制该步骤。

In order to send a PATCH request you can use WebClient.UploadData . 为了发送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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM