【问题标题】:Method Not Allowed when calling Post using the Build api使用 Build api 调用 Post 时方法不允许
【发布时间】:2018-03-14 18:10:32
【问题描述】:

我正在使用 TFS2018 并像这样调用构建 api

internal void UpdateSourceBranches(List<BuildDefinition> defs)
        {
            using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(tfsUser, tfsPass) })
            using (var client = new HttpClient(handler))
            {
                try
                {
                    client.BaseAddress = new Uri(tfsServer);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    foreach (var def in defs)
                    {
                        var buildId = def.Id;
                        var sourceBranch = $"$/{def.Repository.Name}/{def.Project.Name}";
                        var parameters = new Dictionary<string, string> { { "BuildConfiguration", "release" },
                        { "BuildPlatform", "x86|x64|ARM" },
                        { "system.debug", "true" }
                    };

                        var jsonParams = JsonConvert.SerializeObject(parameters);

                        var content = new FormUrlEncodedContent(new[]
                        {
                            new KeyValuePair<string, string>("id", buildId.ToString()),
                            new KeyValuePair<string, string>("sourceBranch", sourceBranch),
                            new KeyValuePair<string, string>("parameters", jsonParams)
                        });

                        var response = client.PostAsync($"DefaultCollection/{def.Repository.Name}/_apis/build/builds?api-version=3.0-preview.1", content);
                        var s = response.Result;
                    }                  
                }
                catch (Exception ex)
                {

                }

            }
        }

但得到以下响应

{StatusCode: 405, ReasonPhrase: '方法不允许', 版本: 1.1, 内容:System.Net.Http.StreamContent,标头:{ Pragma: no-cache X-TFS-ProcessId: ActivityId: X-TFS-Session: X-VSS-E2EID: X-FRAME-OPTIONS: SAMEORIGIN X-VSS-UserData: :user Persistent-Auth: true Lfs-Authenticate: NTLM X-Content-Type-Options: nosniff 缓存控制:无缓存日期:2018 年 3 月 9 日星期五 14:37:16 GMT P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT" 服务器: Microsoft-IIS/10.0 X-AspNet-版本:4.0.30319 X-Powered-By:ASP.NET 内容长度:93 允许:GET 内容类型:应用程序/json; charset=utf-8 过期:-1 }}

知道为什么我得到 Method Not Allowed 吗?

这是不好的回应:

{StatusCode: 400, ReasonPhrase: 'Bad Request', 版本: 1.1, 内容: System.Net.Http.StreamContent,标头:{ Pragma: no-cache
X-TFS-ProcessId:xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx ActivityId: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx X-TFS-会话: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx X-VSS-E2EID: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx X-FRAME-OPTIONS: SAMEORIGIN
X-VSS-UserData: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx:user
Persistent-Auth: true Lfs-Authenticate: NTLM
X-Content-Type-Options:nosniff 缓存控制:无缓存日期:星期二, 2018 年 3 月 13 日 09:21:53 GMT P3P:CP="...多个关键字" 服务器: Microsoft-IIS/10.0 X-AspNet-版本:4.0.30319 X-Powered-By: ASP.NET 内容长度:547 内容类型:应用程序/json; charset=utf-8 过期:-1 }}

【问题讨论】:

    标签: tfs azure-devops


    【解决方案1】:

    听起来您的 POST 是问题所在,请尝试使用 GET

    【讨论】:

    • 是的,你是对的 POST 是问题所在。知道如何解决吗?
    • 使用 GetAsync 代替 PostAsync
    • 好的,但是如果我使用Get怎么传入参数呢?
    • 谢谢,为什么不允许发帖??
    【解决方案2】:

    您应该使用api-version=2.0 而不是3.0-preview.1

    更新代码sn-p:

    string con = "{\"definition\": {\"id\": 47},\"sourceBranch\":\"$/CeceScrum/TestCaseProject\",\"parameters\":\"{\\\"BuildConfiguration\\\":\\\"release\\\",\\\"BuildPlatform\\\":\\\"any cpu\\\",\\\"system.debug\\\":\\\"false\\\"}\"}";
                            var patchValue = new StringContent(con, Encoding.UTF8, "application/json");
                            HttpClient httpClient = new HttpClient();
                            string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "domain\\username", "password")));
                            httpClient.DefaultRequestHeaders.Accept.Clear();
                            httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);
                            var method = new HttpMethod("POST");
    
                            var request = new HttpRequestMessage(method, "http://TFS2018:8080/tfs/DefaultCollection/CeceScrum/_apis/build/builds?api-version=2.0") { Content = patchValue };
    
                            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                            HttpResponseMessage response = httpClient.SendAsync(request).Result;
    
                           string re= response.Content.ReadAsStringAsync().Result;
    

    【讨论】:

    • 嗨 @CeceDong-MSFT 如果我更改为 api-version=2.0 我收到了一个错误的请求...
    • 错误请求意味着您的 api 不正确。您是否要为所有构建定义排队构建?你可以尝试设置一个断点来检查你得到了什么content并分享它?
    • 嗨@CeceDong-MSFT,内容字段中只有基本标题。我添加了对问题的回复...
    • 嗨 @CeceDon-MSFT 现在它可以工作了,但我认为这会修复 stackoverflow.com/questions/49068293/… 。是否也可以填写搁置集?
    • 是的,您在此处指定的sourceBranch 将适用于Shelvset name
    猜你喜欢
    • 2021-09-09
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2017-07-16
    • 2016-02-15
    • 2014-04-18
    • 2021-04-05
    相关资源
    最近更新 更多