2011-08-12 5 views
23

웹 서비스를 테스트하는 진단 도구가 있습니다.WebClient 오류를 문자열로 가져 오기

문제점이있을 때 도구가보고되기를 원합니다. 따라서 테스트를 위해 계약서에 문제가있는 서비스를 배치했습니다.

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is: System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension:
System.ServiceModel.Description.DataContractSerializerOperationBehavior contract: DataContract for type XXX cannot be added to DataContractSet since type XXX with the same data contract name XXX in namespace XXX is already present and the contracts are not equivalent etc..

내가 원하는 것은 전화를 할 수있다 :

myErrorMsg = WebClient.DownloadString("MyBadService.svc"); 

을하고이 유용한 오류 나는 그것을 탐색 할 때

나는 매우 설명과 같은 메시지가있는 페이지를 얻을 수 저도 같은 오류 혼란을 얻을 수있는 방법

The remote server returned an error: (500) Internal Server Error.

: 문자열로 메시지가 그러나 나는 다음 WebException이를 얻을 수 브라우저에서받은 나이가 예외없이 문자열로 반환 되었습니까?

감사합니다.

+0

500 오류는 웹 사이트가 예기치 않게 종료되었음을 의미합니다. – Zenwalker

답변

54

예외를 catch하고 응답을 읽어야합니다.

catch (WebException exception) 
{ 
    string responseText; 

    var responseStream = exception.Response?.GetResponseStream(); 

    if (responseStream != null) 
    { 
     using (var reader = new StreamReader(responseStream)) 
     { 
     responseText = reader.ReadToEnd(); 
     } 
    } 
} 
+1

Hey awesome thanks – lockstock

+4

연결 오류, 이름 확인 오류 등으로 인해 WebException이 발생하는 경우이 오류가 발생합니다. 스트림에서 스트림을 가져 오기 전에 repsonse가 있는지 확인하십시오. – JamieSee

+0

하하! 마침내 해결책을 얻었다. 고마워요. – Anand

관련 문제