HttpClient请求抛出IOException

JFo*_*kes 70 .net c# httpclient ioexception

以下代码抛出IOException并显示以下消息:"指定的注册表项不存在."

HttpClient client = new HttpClient();

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

client.GetAsync(uri);
Run Code Online (Sandbox Code Playgroud)

这只是在Main的控制台应用程序中.看起来mscorlib.dll抛出了错误!Microsoft.Win32.RegistryKey.Win32Error(int errorCode,string str).我不知道为什么抛出这个错误或者如何开始调试它.

编辑堆栈跟踪:

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

它只有1行,没有内在的例外等.

调用堆栈是:

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]  
Run Code Online (Sandbox Code Playgroud)

lig*_*gaz 84

这似乎是由最近的.NET Framework安全更新引起的:MS12-074:.NET Framework中的漏洞可能允许远程执行代码:2012年11月13日(KB 2745030)

这一切归结为Web代理解析中的以下代码:

[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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,它会检查知识库文章中提到的特定注册表项.此外,您应该注意到异常是在内部捕获的,但您可以看到它,因为您已在Visual Studio的调试选项中启用了First Chance Exceptions.

如果您不想看到此异常,则应添加具有值的指定注册表项0:

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

对于64位计算机上的32位进程:

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

  • 发现我的Windows 7 64位盒上的更新已应用为KB2729449.其他系统的相关KB应在此处找到http://technet.microsoft.com/en-us/security/bulletin/ms12-074将LegacyWPADSupport添加到注册表工作. (4认同)

Bil*_*ees 28

我同意Ligaz的回答,并且我已经记录了有关此错误的Connect问题:https://connect.microsoft.com/VisualStudio/feedback/details/773666/webrequest-create-eats-an-ioexception-on-the- 首次呼叫#细节

将以下内容保存到.reg文件中并将其导入注册表以防止引发此错误:

Windows Registry Editor Version 5.00

; The following value prevents an IOException from being thrown and caught
; by System.Net.HybridWebProxyFinder.InitializeFallbackSettings() (in System.dll)
; when WebRequest.Create is first called.  By default the "LegacyWPADSupport"
; value doesn't exist, and when InitializeFallbackSettings calls GetValueKind,
; an IOException is thrown.  This adds the value with its default of false to
; prevent the exception.
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework]
"LegacyWPADSupport"=dword:00000000

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework]
"LegacyWPADSupport"=dword:00000000
Run Code Online (Sandbox Code Playgroud)


use*_*116 10

无论出于何种原因,您的HttpClient代码都在注册表中查找代理设置,无法打开密钥.查看代码显示它尝试打开HKCU,然后按顺序转到以下某个键:

  1. "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections"
  2. "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections"
  3. "HKLM\\SOFTWARE\\Policies\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"

这三个中的一个可能是您的进程无法访问的密钥,为什么我不确定.一种可能的解决方法是禁用自动检测代理设置.

否则,你需要确切地知道它正在加载什么键,我们将通过两个步骤来完成.

  1. 启用S​​ystem.Net日志记录.
  2. 下载并运行Procmon,过滤应用程序的注册表访问,如下所示:
  1. 打开后,如果启用则禁用捕获(放大镜应该有一个红色的X).在此输入图像描述
  2. 开始过滤您的进程名称.在此输入图像描述
  3. 取消选择除注册表项以外的所有选项

在此输入图像描述

  1. 启用捕获(单击放大镜)
  2. 运行您的应用程序
  3. 在日志中找到有问题的条目,双击以查看它正在打开哪个键

确定有问题的密钥后,您可以找出应用程序无法访问它的原因.也许,如果您的应用程序的名称是任何指示,则您的服务运行的用户帐户无法访问注册表项.