[c#] WebException 본문으로 전체 응답을 얻는 방법은 무엇입니까?

WebException에서 GetResponse의 본문을 볼 수 없습니다. 이것은 C #의 내 코드입니다.

try {
  return GetResponse(url + "." + ext.ToString(), method, headers, bodyParams);
} catch (WebException ex) {
    switch (ex.Status) {
      case WebExceptionStatus.ConnectFailure:
         throw new ConnectionException();
     case WebExceptionStatus.Timeout:
         throw new RequestTimeRanOutException();
     case WebExceptionStatus.NameResolutionFailure:
         throw new ConnectionException();
     case WebExceptionStatus.ProtocolError:
          if (ex.Message == "The remote server returned an error: (401) unauthorized.") {
              throw new CredentialsOrPortalException();
          }
          throw new ProtocolErrorExecption();
     default:
          throw;
    }

헤더는 보이지만 본문은 보이지 않습니다. 요청에 대한 Wireshark의 출력입니다.

POST /api/1.0/authentication.json HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Host: nbm21tm1.teamlab.com
Content-Length: 49
Connection: Keep-Alive

userName=XXX&password=YYYHTTP/1.1 500 Server error
Cache-Control: private, max-age=0
Content-Length: 106
Content-Type: application/json; charset=UTF-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
X-Powered-By: ARR/2.5

Date: Mon, 06 Aug 2012 12:49:41 GMT
Connection: close

{"count":0,"startIndex":0,"status":1,"statusCode":500,"error":{"message":"Invalid username or password."}}

WebException에서 메시지 텍스트를 볼 수 있습니까? 감사합니다.



답변

var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();

dynamic obj = JsonConvert.DeserializeObject(resp);
var messageFromServer = obj.error.message;


답변

try {
 WebClient client = new WebClient();
 client.Encoding = Encoding.UTF8;
 string content = client.DownloadString("https://sandiegodata.atlassian.net/wiki/pages/doaddcomment.action?pageId=524365");
 Console.WriteLine(content);
 Console.ReadKey();
} catch (WebException ex) {
 var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
 Console.WriteLine(resp);
 Console.ReadKey();
}


답변

이것은 기존 답변에서만 향상됩니다. 나는 응답 본문을 포함하는 향상된 메시지로 던지기 / 다시 던지기의 세부 사항을 처리하는 메서드를 작성했습니다.

내 코드는 다음과 같습니다 (Client.cs).

/// <summary>
///     Tries to rethrow the WebException with the data from the body included, if possible. 
///     Otherwise just rethrows the original message.
/// </summary>
/// <param name="wex">The web exception.</param>
/// <exception cref="WebException"></exception>
/// <remarks>
///     By default, on protocol errors, the body is not included in web exceptions. 
///     This solutions includes potentially relevant information for resolving the
///     issue.
/// </remarks>
private void ThrowWithBody(WebException wex) {
    if (wex.Status == WebExceptionStatus.ProtocolError) {
        string responseBody;
        try {
            //Get the message body for rethrow with body included
            responseBody = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();

        } catch (Exception) {
            //In case of failure to get the body just rethrow the original web exception.
            throw wex;
        }

        //include the body in the message
        throw new WebException(wex.Message + $" Response body: '{responseBody}'", wex, wex.Status, wex.Response);
    }

    //In case of non-protocol errors no body is available anyway, so just rethrow the original web exception.
    throw wex;
}

OP가 보여준 것처럼 catch 절에서 사용합니다.

//Execute Request, catch the exception to eventually get the body
try {
    //GetResponse....
    }
} catch (WebException wex) {
    if (wex.Status == WebExceptionStatus.ProtocolError) {
        ThrowWithBody(wex);
    }

    //otherwise rethrow anyway
    throw;
}


답변