如何在wpf应用程序内设置system.net设置

如何在wpf应用程序内设置system.net设置,wpf,app-config,webclient,system.net,Wpf,App Config,Webclient,System.net,在WPF项目中,我遇到以下错误: private void btnRetrieve_Click(object sender, RoutedEventArgs e) { Uri uri = new Uri("http://10.214.36.245", UriKind.Absolute); using (WebClient wc = new WebClient()) { byte[] barr = wc.DownloadData(uri);

在WPF项目中,我遇到以下错误:

private void btnRetrieve_Click(object sender, RoutedEventArgs e)
{
    Uri uri = new Uri("http://10.214.36.245", UriKind.Absolute);
    using (WebClient wc = new WebClient())
    {
        byte[] barr = wc.DownloadData(uri);
        string sData = new string(barr.Select(a => Convert.ToChar(a)).ToArray());
        tbRetrievedData.Text = sData;
    }
}

在Windows窗体应用程序中也是如此:

private void btnClicked(object sender, EventArgs e)
{
    WebClient wc = new WebClient();
    var c = wc.DownloadData("http://10.214.36.245");
}

解决办法就在这里

在windows窗体应用程序中,很容易:

但我不知道如何在WPF应用程序中实现这个解决方案。即使我添加了app.config文件并根据解决方案更改了其内容,也会显示以下错误:

private void btnRetrieve_Click(object sender, RoutedEventArgs e)
{
    Uri uri = new Uri("http://10.214.36.245", UriKind.Absolute);
    using (WebClient wc = new WebClient())
    {
        byte[] barr = wc.DownloadData(uri);
        string sData = new string(barr.Select(a => Convert.ToChar(a)).ToArray());
        tbRetrievedData.Text = sData;
    }
}

PS:如下所示,但我不想使用此解决方案,因为它不允许我在WPF应用程序中使用App.config文件:

public static bool SetAllowUnsafeHeaderParsing20()
{
    //Get the assembly that contains the internal class
    Assembly aNetAssembly = Assembly.GetAssembly(typeof(SettingsSection));
    if (aNetAssembly != null)
    {
        //Use the assembly in order to get the internal type for the internal class
        Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
        if (aSettingsType != null)
        {
            //Use the internal static property to get an instance of the internal settings class.
            //If the static instance isn't created allready the property will create it for us.
            object anInstance = aSettingsType.InvokeMember("Section", BindingFlags.Static |
                                                                        BindingFlags.GetProperty |
                                                                        BindingFlags.NonPublic, null, null,
                                                            new object[] { });
            if (anInstance != null)
            {
                //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
                FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
                if (aUseUnsafeHeaderParsing != null)
                {
                    aUseUnsafeHeaderParsing.SetValue(anInstance, true);
                    return true;
                }
            }
        }
    }
    return false;
}