C# HttpClient HttpRequestException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15143107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpClient HttpRequestException
提问by Lord Vermillion
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.
我遇到了问题HttpClient
,我不断收到HttpRequestException occurred in mscorlib.dll
。我已经从 SVN 下载了以前的工作版本,但我一直收到相同的异常。
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:
我的innerException如下:
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:
对于我尝试的每个 url,我都得到相同的结果,并且它们都可以在我的浏览器中运行。这是异常和堆栈跟踪:
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)
回答by Charlie Kilian
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.
这是您网络上的 DNS 问题。您的工作站(或收到错误的服务器)无法解析 DNS 名称“api.themoviedb.org”。这个 DNS 名称从我的计算机和其他网络在线解析得很好。
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.
因此,您的机器可以解析名称,但 HttpClient 不能。让我们看看我们是否可以解决这个问题。我将假设这是一个桌面应用程序,而不是一个网站。如果我错了,请告诉我——还有一些额外的注意事项。
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:
您将必须按照本指南启用网络跟踪:如何:配置网络跟踪。您希望按照该链接中的指定配置您的 App.config 文件。简而言之,您应该在配置文件中添加如下内容:
<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.
network.log
应更改该值以满足您的需要。继续这样做,并查看输出文件。
Now, the trick is using that file to figure out why there is a problem. See Interpreting Network Tracingfor 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.
现在,诀窍是使用该文件找出出现问题的原因。有关如何解释该文件的提示,请参阅解释网络跟踪,尽管信息很少。我们正在寻找的是无法解析 DNS 的原因。因此,在该文件中搜索“api.themoviedb.org”。如果您愿意,请使用结果更新问题,我会看看是否可以从那里帮助您。