HttpClient的请求抛出IOException异常 [英] HttpClient request throws IOException

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

问题描述

下面code抛出IOException异常的消息:指定的注册表项不存在。

  HttpClient的客户端=新的HttpClient();

开放的我们的uri =新的URI(http://www.google.com);

client.GetAsync(URI);
 

这仅仅是在主控制台应用程序。它看起来像错误被抛出mscorlib.dll中!Microsoft.Win32.RegistryKey.Win32Error(INT错误code,字符串str)。我不知道为什么这个错误被抛出或如何开始调试了。

编辑堆栈跟踪:

在Microsoft.Win32.RegistryKey.Win32Error(的Int32错误code,字符串str)

这只是1行,没有内在的exxception等。

调用堆栈是:

  mscorlib.dll中!Microsoft.Win32.RegistryKey.Win32Error(INT错误code,字符串str)+ 0x189字节
mscorlib.dll中!Microsoft.Win32.RegistryKey.GetValueKind(字符串名称)+ 0x7f的字节
System.dll中!System.Net.HybridWebProxyFinder.InitializeFallbackSettings()+ 0x9e字节
[原产于托管过渡]
[托管到本机转换]
System.dll中!System.Net.AutoWebProxyScriptEngine.AutoWebProxyScriptEngine(System.Net.WebProxy代理,布尔useRegistry)+ 0xd0字节
System.dll中!System.Net.WebProxy.UnsafeUpdateFromRegistry()+ 0x2c字节
System.dll!System.Net.Configuration.DefaultProxySectionInternal.DefaultProxySectionInternal(System.Net.Configuration.DefaultProxySection部分)+ 0x1d8字节
System.dll中!System.Net.Configuration.DefaultProxySectionInternal.GetSection()+ 0xec字节
System.dll中!System.Net.WebRequest.InternalDefaultWebProxy.get()+的0xCC字节
System.dll中!System.Net.HttpWebRequest.HttpWebRequest(URI的System.Uri,System.Net.ServicePoint服务点)+ 0xdf字节
!System.dll中System.Net.HttpWebRequest.HttpWebRequest(URI的System.Uri,布尔returnResponseOnFailureStatus code,串connectionGroupName,System.Action<的System.IO.Stream> resendRequestContent)+ 0x2B访问字节
System.Net.Http.dll!System.Net.Http.HttpClientHandler.CreateAnd$p$ppareWebRequest(System.Net.Http.Htt$p$pquestMessage请求)+ 0x59字节
System.Net.Http.dll!System.Net.Http.HttpClientHandler.SendAsync(System.Net.Http.Htt prequestMessage要求,System.Threading.CancellationToken的CancellationToken)+ 0xF4中的字节
System.Net.Http.dll!System.Net.Http.HttpMessageInvoker.SendAsync(System.Net.Http.Htt prequestMessage要求,System.Threading.CancellationToken的CancellationToken)+ 0x4f字节
System.Net.Http.dll!System.Net.Http.HttpClient.SendAsync(System.Net.Http.Htt prequestMessage要求,System.Net.Http.HttpCompletionOption completionOption,System.Threading.CancellationToken的CancellationToken)+ 0x13e字节
System.Net.Http.dll!System.Net.Http.HttpClient.GetAsync(的System.Uri requestUri,System.Net.Http.HttpCompletionOption completionOption)+ 0xC字节

ConsoleServiceTest.exe!ConsoleServiceTest.Program.Main(字串[] args)第20行+ 0x17已字节C#
    [原产于托管过渡]
    [托管到本机转换]
    Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() +字节5AH即可
    mscorlib.dll中!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext的ExecutionContext,System.Threading.ContextCallback回调,对象的状态,布尔preserveSyncCtx)+ 0x285字节
    mscorlib.dll中!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext的ExecutionContext,System.Threading.ContextCallback回调,对象的状态,布尔preserveSyncCtx)+ 0x9字节
    mscorlib.dll中!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext的ExecutionContext,System.Threading.ContextCallback回调,对象的状态)+ 0×57字节
    mscorlib.dll中!System.Threading.ThreadHelper.ThreadStart()+ 0x51字节
    [原产于托管过渡]
 

解决方案

看来,这是由.NET Framework的一个最新的安全更新:的 MS12-074:.NET Framework中的漏洞可能允许远程code执行:2012年11月13日(KB 2745030)

这一切都归结到Web代理解析以​​下code:

  [RegistryPermission的(SecurityAction.Assert,读= @HKEY_LOCAL_MACHINE \ SOFTWARE \微软\ .NETFramework)
私有静态无效InitializeFallbackSettings()
{
    allowFallback = FALSE;
    尝试
    {
        使用(的RegistryKey键= Registry.LocalMachine.OpenSubKey(@SOFTWARE \微软\ .NETFramework))
        {
            尝试
            {
                如果(key.GetValueKind(LegacyWPADSupport)== RegistryValueKind.DWord)
                {
                    allowFallback =((int)的key.GetValue(LegacyWPADSupport))== 1;
                }
            }
            赶上(UnauthorizedAccessException)
            {
            }
            赶上(IOException异常)
            {
            }
        }
    }
    赶上(SecurityException异常)
    {
    }
    赶上(的ObjectDisposedException)
    {
    }
}
 

正如你可以看到它检查的知识库文章中提到特定的注册表项。你也应该注意的是,除了在内部抓,但你看它,因为你已经启用了第一次机会异常在Visual Studio中的调试选项。

如果你想不看到这个异常你应该增加值为 0 指定的注册表项:

 注册表位置:HKEY_LOCAL_MACHINE \ SOFTWARE \微软\ .NETFramework
DWORD(32-位)值名称:LegacyWPADSupport
值数据:0
 

和64位机:

 注册表位置:HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \微软\ .NETFramework
DWORD(32-位)值名称:LegacyWPADSupport
值数据:0
 

The following code throws a IOException with the message: "The specified registry key does not exist."

HttpClient client = new HttpClient();

Uri uri = new Uri("http://www.google.com");

client.GetAsync(uri);

This is just in a console app in Main. It looks like the error is being thrown by mscorlib.dll!Microsoft.Win32.RegistryKey.Win32Error(int errorCode, string str). I have no idea why this error is being thrown or how to start debugging it.

Edit stack trace:

at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)

It's just 1 line and there is no inner exxception etc..

The call stack is:

mscorlib.dll!Microsoft.Win32.RegistryKey.Win32Error(int errorCode, string str) + 0x189 bytes    
mscorlib.dll!Microsoft.Win32.RegistryKey.GetValueKind(string name) + 0x7f bytes 
System.dll!System.Net.HybridWebProxyFinder.InitializeFallbackSettings() + 0x9e bytes    
[Native to Managed Transition]  
[Managed to Native Transition]  
System.dll!System.Net.AutoWebProxyScriptEngine.AutoWebProxyScriptEngine(System.Net.WebProxy proxy, bool useRegistry) + 0xd0 bytes   
System.dll!System.Net.WebProxy.UnsafeUpdateFromRegistry() + 0x2c bytes  
System.dll!System.Net.Configuration.DefaultProxySectionInternal.DefaultProxySectionInternal(System.Net.Configuration.DefaultProxySection section) + 0x1d8 bytes 
System.dll!System.Net.Configuration.DefaultProxySectionInternal.GetSection() + 0xec bytes   
System.dll!System.Net.WebRequest.InternalDefaultWebProxy.get() + 0xcc bytes 
System.dll!System.Net.HttpWebRequest.HttpWebRequest(System.Uri uri, System.Net.ServicePoint servicePoint) + 0xdf bytes  
System.dll!System.Net.HttpWebRequest.HttpWebRequest(System.Uri uri, bool returnResponseOnFailureStatusCode, string connectionGroupName, System.Action<System.IO.Stream> resendRequestContent) + 0x2b bytes  
System.Net.Http.dll!System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequest(System.Net.Http.HttpRequestMessage request) + 0x59 bytes   
System.Net.Http.dll!System.Net.Http.HttpClientHandler.SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) + 0xf4 bytes  
System.Net.Http.dll!System.Net.Http.HttpMessageInvoker.SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) + 0x4f bytes 
System.Net.Http.dll!System.Net.Http.HttpClient.SendAsync(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) + 0x13e bytes 
System.Net.Http.dll!System.Net.Http.HttpClient.GetAsync(System.Uri requestUri, System.Net.Http.HttpCompletionOption completionOption) + 0xc bytes   

ConsoleServiceTest.exe!ConsoleServiceTest.Program.Main(string[] args) Line 20 + 0x17 bytes  C#
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x5a bytes  
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x285 bytes 
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x9 bytes   
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x57 bytes    
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x51 bytes   
    [Native to Managed Transition]  

解决方案

It seems that this is caused by a recent security update for the .NET Framework: MS12-074: Vulnerabilities in .NET Framework could allow remote code execution: November 13, 2012 (KB 2745030)

It all boils down to the following code in the web proxy resolution:

[RegistryPermission(SecurityAction.Assert, Read=@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework")]
private static void InitializeFallbackSettings()
{
    allowFallback = false;
    try
    {
        using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\.NETFramework"))
        {
            try
            {
                if (key.GetValueKind("LegacyWPADSupport") == RegistryValueKind.DWord)
                {
                    allowFallback = ((int) key.GetValue("LegacyWPADSupport")) == 1;
                }
            }
            catch (UnauthorizedAccessException)
            {
            }
            catch (IOException)
            {
            }
        }
    }
    catch (SecurityException)
    {
    }
    catch (ObjectDisposedException)
    {
    }
}

As you can see it checks for a specific registry key mentioned in the KB article. Also you should note that the exception is caught internally, but you see it because you have enabled First Chance Exceptions in the debug options of Visual Studio.

If you want to not see this exception you should add the specified registry key with value 0:

Registry location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework  
DWORD (32-bit) Value name: LegacyWPADSupport
Value data: 0

and for 64bit machines:

Registry location: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework
DWORD (32-bit) Value name: LegacyWPADSupport
Value data: 0

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

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