2013-02-28 57 views
4

我遇到了問題HttpClient,我不斷收到HttpRequestException occurred in mscorlib.dll。我已經從SVN下載了以前的工作版本,並且不斷收到相同的異常。HttpClient HttpRequestException

爲什麼我突然得到這個異常,即使我正在運行一直工作正常的數週的代碼?

現在,我想用這個簡單的代碼進行調試,但我得到了同樣的異常無論我做什麼

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如下:

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

我得到相同對於我嘗試的每個網址,他們都在我的瀏覽器中工作。 這裏是例外,堆棧跟蹤:

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) 
+1

什麼是完整的異常錯誤? – ChrisBint 2013-02-28 18:41:01

+0

您使用的是代理服務器嗎? – ChrisBint 2013-02-28 18:42:54

+0

水庫\t「System.Net.Http.HttpRequestException:發送請求時發生錯誤---> System.Net.WebException:遠程名稱無法解析:api.themoviedb.org'\ r \ n在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\ r \ n在System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)\ r \ n ---內部異常堆棧跟蹤結束--- \ r \ n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n – 2013-02-28 18:45:27

回答

7

這是您的網絡上的DNS問題。您的工作站(或發生錯誤的服務器)無法解析DNS名稱「api.themoviedb.org」。此DNS名稱可以解決我的計算機和other networks online的罰款。

請與您的本地網絡管理員。

UPDATE

所以,你的機器可以解析該名稱,但HttpClient的不能。讓我們看看我們是否可以解決這個問題。我會假設這是一個桌面應用程序,而不是一個網站。如果我錯了,請告訴我 - 還有一些額外的考慮因素。

您必須按照本指南啓用網絡跟蹤:How to: Configure Network Tracing。您想要按照該鏈接中指定的方式配置您的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> 

network.log應更改爲滿足您的需求。繼續並做到這一點,並看看輸出文件。

現在,訣竅是使用該文件找出爲什麼有問題。有關如何解釋該文件的提示,請參見Interpreting Network Tracing,儘管信息很稀少。我們正在尋找的是DNS無法解決的原因。所以,搜索該文件的「api.themoviedb.org」。如果你願意,可以用結果更新問題,我會看看我能否幫你解決問題。

+0

它適用於我的瀏覽器,但不是當我運行代碼時。有任何想法嗎? – 2013-02-28 22:03:26

+0

打開命令提示符並運行命令「ping api.themoviedb.org」。它是否解析爲IP地址? – 2013-02-28 22:05:44

+0

它顯示IP但所有請求超時。 – 2013-02-28 22:34:15

相關問題