简体   繁体   English

VSO REST API创建工作项错误404

[英]VSO REST API Create work item error 404

I'm trying to create a work item with this code : 我正在尝试使用以下代码创建工作项:

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://xxx.visualstudio.com/defaultcollection/xxx/_apis/wit/workitems/$Bug?api-version=1.0");
httpWebRequest.ContentType = "application/json-patch+json";
httpWebRequest.Credentials = new NetworkCredential("me",Settings.Default.token);
httpWebRequest.Method = "PATCH";
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new
{
    op="add",
    path= "/fields/System.Title",
    value="New bug from application"
});
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    streamWriter.Write(json);
}
HttpWebResponse httpResponse = null;
try { httpResponse = (HttpWebResponse) httpWebRequest.GetResponse(); }
catch (WebException e)
{
    //Exception catched there, error 404
    Console.WriteLine(e.Status); //Writes ProtocolError
    throw;
}
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    string responseText = streamReader.ReadToEnd();
    Console.WriteLine(responseText);
}

But I get an error 404 (Not Found). 但我收到错误404(未找到)。 When i change the method(to PUT or POST) I get a code for a visual studio page. 当我更改方法(更改为PUT或POST)时,我得到了Visual Studio页面的代码。

When i'm going to https://xxx.visualstudio.com/defaultcollection/xxx/_apis/wit/workitems/$Bug?api-version=1.0 i can see some json. 当我转到https://xxx.visualstudio.com/defaultcollection/xxx/_apis/wit/workitems/$Bug?api-version=1.0时,我可以看到一些json。 So this is not an URL error. 因此,这不是URL错误。

I finally can do what I want : 我终于可以做我想做的:

public async Task CreateBug(Bug bug)
{
    string token = Settings.Default.token;
    string requestUrl = "https://xxx.visualstudio.com/defaultcollection/xxx/_apis/wit/workitems/$Bug?api-version=1.0";
    HttpClientHandler httpClientHandler = new HttpClientHandler
    {
        Proxy = this.GetProxy(),
        UseProxy = true,
        UseDefaultCredentials = true
    };
    HttpClient httpClient = new HttpClient(httpClientHandler);
    httpClient.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
        Convert.ToBase64String(
            System.Text.ASCIIEncoding.ASCII.GetBytes(
                string.Format("{0}:{1}", "me", token))));
    var method = new HttpMethod("PATCH");
    var request = new HttpRequestMessage(method, requestUrl)
    {
        Content = new StringContent(GetStrJsonData(), Encoding.UTF8,
            "application/json-patch+json")
    };
    HttpResponseMessage hrm = await httpClient.SendAsync(request);
    Response = hrm.Content;
}

I've used an HttpClient instead of an HttpWebRequest 我使用了HttpClient而不是HttpWebRequest

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

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