Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient HttpRequestException

Tags:

c#

httpclient

I'm having a problem with HttpClient, I keep getting HttpRequestException occurred in mscorlib.dll. I've downloaded previous working versions from SVN and I keep getting the same Exception.

Why am I suddenly getting this exception, even when I'm running code that has been working fine for weeks?

Right now I'm trying to debug with this simple code, but I get the same exception whatever I do

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept
    .Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(
              "application/json"));
HttpResponseMessage response
    = await client.GetAsync("http://api.themoviedb.org/3/movie/550?api_key=x");
return await response.Content.ReadAsStringAsync();

My innerException is the following:

The remote name could not be resolved: 
    api.themoviedb.org System.Exception System.Net.WebException

I get the same for every url I try and they all work in my browser. Here is the exception and stack trace:

res "System.Net.Http.HttpRequestException: An error occurred while sending the request. 
---> System.Net.WebException: The remote name could not be resolved: 'api.themoviedb.org'
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificat‌​ion(Task task)
like image 469
Lord Vermillion Avatar asked Feb 28 '13 18:02

Lord Vermillion


People also ask

What is HttpRequestException?

HttpRequestException Class: A base class for exceptions thrown by the HttpClient and HttpMessageHandler classes.

What is HttpRequestException in C#?

HttpRequestException(String, Exception) Initializes a new instance of the HttpRequestException class with a specific message that describes the current exception and an inner exception.


1 Answers

This is a DNS problem on your network. Your workstation (or the server that is getting the error) is unable to resolve the DNS name 'api.themoviedb.org'. This DNS name resolves fine from my computer and from other networks online.

Check with your local network admin.

UPDATE

So, your machine can resolve the name, but the HttpClient can't. Let's see if we can resolve this problem. I'm going to assume this is a desktop application, not a website. If I'm wrong, let me know -- there are some additional considerations.

You're going to have to enable network tracing following this guide: How to: Configure Network Tracing. You want to configure your App.config file as specified at that link. In short, you should add something like this to your config file:

<configuration>
    <system.diagnostics>
        <sources>
            <source name="System.Net" tracemode="includehex" maxdatasize="1024">
                <listeners>
                    <add name="System.Net"/>
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="System.Net" value="Verbose"/>
        </switches>
        <sharedListeners>
            <add name="System.Net"
                type="System.Diagnostics.TextWriterTraceListener"
                initializeData="network.log" />
        </sharedListeners>
        <trace autoflush="true"/>
    </system.diagnostics>
</configuration>

The value network.log should be changed to suit your needs. Go ahead and do that, and take a look at the output file.

Now, the trick is using that file to figure out why there is a problem. See Interpreting Network Tracing for tips on how to interpret that file, though info is sparse. What we are looking for is the reason the DNS wasn't able to be resolved. So, search that file for "api.themoviedb.org". If you'd like, update the question with the results, and I'll see if I can help you out from there.

like image 80
Katie Kilian Avatar answered Oct 17 '22 21:10

Katie Kilian