HttpClient HttpRequestException [英] HttpClient HttpRequestException

查看:291
本文介绍了HttpClient HttpRequestException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HttpClient上遇到问题,我一直在获取HttpRequestException occurred in mscorlib.dll.我已经从SVN下载了以前的工作版本,并且不断收到相同的异常.

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();

我的innerException如下:

My innerException is the following:

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

我尝试的每个URL都具有相同的含义,它们都可以在我的浏览器中使用. 这是异常和堆栈跟踪:

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)

推荐答案

这是您网络上的DNS问题.您的工作站(或出现错误的服务器)无法解析DNS名称"api.themoviedb.org".从我的计算机和其他在线网络可以很好地解析此DNS名称.

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.

更新

因此,您的计算机可以解析名称,但是HttpClient不能.让我们看看是否可以解决此问题.我将假定这是一个桌面应用程序,而不是网站.如果我错了,请告诉我-还有其他注意事项.

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.

您将必须按照本指南启用网络跟踪:如何:配置网络跟踪.您要配置该链接中指定的App.config文件.简而言之,您应该在配置文件中添加以下内容:

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>

应更改值network.log以适合您的需求.继续执行此操作,然后查看输出文件.

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

现在,诀窍是使用该文件来找出出现问题的原因.请参阅解释网络跟踪,以获取有关如何解释该文件的提示,尽管信息很少.我们正在寻找的是无法解析DNS的原因.因此,在该文件中搜索"api.themoviedb.org".如果您愿意,请使用结果更新问题,我会看看是否可以为您提供帮助.

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.

这篇关于HttpClient HttpRequestException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆