{"id":5403,"date":"2004-08-13T17:01:00","date_gmt":"2004-08-13T17:01:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/buckh\/2004\/08\/13\/handling-http-response-codes-in-soap-proxies\/"},"modified":"2004-08-13T17:01:00","modified_gmt":"2004-08-13T17:01:00","slug":"handling-http-response-codes-in-soap-proxies","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/buckh\/handling-http-response-codes-in-soap-proxies\/","title":{"rendered":"Handling HTTP response codes in SOAP proxies"},"content":{"rendered":"<p>Continuing with the last post&nbsp;on&nbsp;<A href=\"http:\/\/blogs.msdn.com\/buckh\/archive\/2004\/08\/10\/211984.aspx\">authentication issues with wsdl.exe-generated SOAP<\/a> proxies, you may need to handle HTTP status codes to provide custom exceptions.&nbsp; To do this, the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemwebservicesprotocolssoaphttpclientprotocolclasstopic.asp\">SoapHttpClientProtocol<\/a> class (actually its parent <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemwebservicesprotocolshttpwebclientprotocolclasstopic.asp\">HttpWebClientProtocol<\/a>) provides a pair of <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemwebservicesprotocolshttpwebclientprotocolclasstopic.asp\">GetWebResponse()<\/a> methods for derived classes to override.&nbsp; One method is for synchronous calls and the other for asynchronous.<\/p>\n<p>The following code shows how to generate a custom exception for the HTTP 401 Unauthorized code.<\/p>\n<pre>using System;<br \/>using System.Net;<br \/> <br \/>public class MyProxy : WsdlExeGeneratedProxy<br \/>{<br \/>&nbsp;&nbsp;&nbsp;protected override WebRequest GetWebRequest(Uri uri)<br \/>&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpWebRequest request = base.GetWebRequest(uri) as HttpWebRequest;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.Headers.Add(\"accept-language\", CultureInfo.CurrentUICulture.Name);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.Credentials = m_state.Credentials;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return request;<br \/>&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;protected override WebResponse GetWebResponse(WebRequest request)<br \/>&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpWebResponse response = base.GetWebResponse(request) as HttpWebResponse;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ThrowIfUnauthorized(response);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return response;<br \/>&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)<br \/>&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpWebResponse response = base.GetWebResponse(request, result) as HttpWebResponse;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ThrowIfUnauthorized(response);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return response;<br \/>&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;private void ThrowIfUnauthorized(HttpWebResponse response)<br \/>&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ There are cases where response can be null (dropped network connection is one).<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (response != null &amp;&amp; response.StatusCode == HttpStatusCode.Unauthorized)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new UnauthorizedException(response);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;internal SomeObject m_state;<br \/>}<\/pre>\n<pre>public class UnauthorizedException : ApplicationException<br \/>{<br \/>&nbsp;&nbsp;&nbsp;public UnauthorizedException(HttpWebResponse response)<br \/>&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_response = response;<br \/>&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;internal HttpWebResponse m_response;<br \/>}<br \/> <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Continuing with the last post&nbsp;on&nbsp;authentication issues with wsdl.exe-generated SOAP proxies, you may need to handle HTTP status codes to provide custom exceptions.&nbsp; To do this, the SoapHttpClientProtocol class (actually its parent HttpWebClientProtocol) provides a pair of GetWebResponse() methods for derived classes to override.&nbsp; One method is for synchronous calls and the other for asynchronous. The [&hellip;]<\/p>\n","protected":false},"author":94,"featured_media":10268,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-5403","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-c"],"acf":[],"blog_post_summary":"<p>Continuing with the last post&nbsp;on&nbsp;authentication issues with wsdl.exe-generated SOAP proxies, you may need to handle HTTP status codes to provide custom exceptions.&nbsp; To do this, the SoapHttpClientProtocol class (actually its parent HttpWebClientProtocol) provides a pair of GetWebResponse() methods for derived classes to override.&nbsp; One method is for synchronous calls and the other for asynchronous. The [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/5403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/users\/94"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/comments?post=5403"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/5403\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media\/10268"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media?parent=5403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/categories?post=5403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/tags?post=5403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}