0

我正在开发一个小型应用程序,它从给定的问题 ID 列表中从 JIRA 中提取问题,但是其中一些不存在并且 API 调用返回 400 Bad Request 消息。

这是我到目前为止的相关代码块(C#):

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(thejiraurl);
request.Method = "GET";
request.ContentType = "Content-Type: application/json";
request.Accept = "application/json";
string mergedCredentials = "thecredentials :)";
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
string encodedCredentials = Convert.ToBase64String(byteCredentials);
request.Headers.Add("Authorization", "Basic " + encodedCredentials);

try
{
  WebResponse response = request.GetResponse(); //this line breaks because some records don't exist anymore, that's ok, but at least I need to know which ones! those are found in the response that I can't see here but I can with Fiddler! @.@
  Stream data = response.GetResponseStream();
}
catch(WebException ex)
{
  Console.WriteLine(ex.Message);
  if(ex.Status == WebExceptionStatus.ProtocolError)
  {
    Console.WriteLine("Status Code : {0}", ((HttpWebResponse)ex.Response).StatusCode); //This is 'BadRequest'
    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)ex.Response).StatusDescription); //This is 'Bad Request'
    using (Stream responseStream = ex.Response.GetResponseStream())
    using (StreamReader responseReader = new StreamReader(responseStream))
    {
      string responseMessage = responseReader.ReadToEnd(); //This is a long message but do not has the errormessages seen with Fiddler (and even in a internet browser)
      Console.WriteLine(responseMessage);
    }
  }
}

但是,当我使用 Fiddler 时,我可以看到返回的实际错误消息,如下所示:

{"errorMessages":["A value with ID '1' does not exist for the field 'key'."],"errors":{}}

如何使用 C# 或 vb 获取此消息?

这与问题非常相似,但与 Java 相关:https ://answers.atlassian.com/questions/147793/retrieving-errormessages-when-a-call-to-the-rest-api-fails-using- httpurl连接

我很感激任何帮助。

4

0 回答 0