在 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");
}
解决方案是在这个服务器犯了协议冲突。Section=ResponseStatusLine 错误。
在 windows 窗体应用程序中很容易:
但我不知道如何将此解决方案应用于 WPF 应用程序。即使我添加了 app.config 文件并根据解决方案更改了它的内容,它也会显示此错误:
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;
}