How to set system.net settings inside wpf application

765 views Asked by At

In WPF project I am getting this error:

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;
    }
}

The server committed a protocol violation. Section=ResponseStatusLine

It is same in Windows Form application:

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

Same error

Solution is in this The server committed a protocol violation. Section=ResponseStatusLine ERROR.

In windows form application it is easy: Application Configuration File

But I don't know how to implement this solution to WPF application. Even if I added the app.config file and changed it's content according to the solution it shows this error:

App.Config Added but it is giving same error

PS: It's runtime code is below but I don't want to use this solution because it doesn't give me using of the App.config file in WPF application :

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;
}
0

There are 0 answers