![](/img/trans.png)
[英]How to make an HTTP Patch request from C# with a parameter [FromBody]
[英]How do you send a patch request from a c# client?
我有一個執行此操作的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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.