c# - How to get http response Body when the status code is not valid? -


I am trying to get an html page content from a personal service. The page position code returns 0, which is not valid. But render the page when browsing through a browser.

Try using the WebResponse.GetResponseStream () when I just give it an empty stream. And when I try to use HttpClient.GetStringAsync (url) .Result, throw AggregateException as follow:

  System.AggregateException without HRESULT = -2146233088 Message = One or more errors occurred Source = mscorlib StackTrace: on the system. threading. Tasks. Tasks. Tasks. The trophy system (boolean included in the cancellation canceled) system. threading. Tasks. Task 1 On the system Diagnostic Infocroller.program.man (Sting [] AGR) in C: \ TFS \ MSNMetro \ Tools \ Verticals \ Sports \ DiagnosticInfoCrawler \ Program.cs: System 47 on line 47. AppDomain._nExecuteAssembly (RuntimeAssembly assembly, string [] args) on the system .AppDomain.ExecuteAssembly (String assemblyFile, Evidence AssemblySecurity, string [] args) on Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly () on System.Threading.ThreadHelper.ThreadStart_Context System (object state) system threading. Execution Contact ExecutionContext, ContextCallback Callback, Object State, Boolean preserveSyncCtx) on System.Thre on RunInternal (ExecutionContext execution execution, context callback execution, object state, Boolean protected sync, on ExecutionContext System.Threading.ExecutionContext.Run) ading.ExecutionContext.Run (ExecutionContext execution Context, Context Callback, Callback, Object State). threading. ThreadHalper ThreadStart () Inner expansion: System. met. Hpt HTTPRequestexception HRsult = -2146233088 Message = Response status code does not indicate success: 0 (). InnerException:   

There are two types of code used as follows:

  WebResponse response = GetWebResponse (URL); ReactionBody = (New Stream Reader (Response GaterespanSream)). Reddend (); HTTP Client HTTP Client = New HTTP Client (); httpClient.BaseAddress = new URI (URL); Page Source = httpClient.GetStringAsync (url) result;   

Anyone can advise how can I get feedback body? (Given that I have no control over how to fix the service back to the correct state code.)

Thanks, DD

Hold WebException to get feedback. This is a bad design decision from Microsoft, because non-200 codes can be part of the general case.

  try {WebResponse response = GetWebResponse (url); ReactionBody = (New Stream Reader (Response GaterespanSream)). Reddend (); HTTP Client HTTP Client = New HTTP Client (); HttpClient.BaseAddress = New URI (URL); Page Source = httpClient.GetStringAsync (url) result; } Hold (web exception exception) {var response = (HttpWebResponse) exception. GetResponse (); // response is here ..}    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -