HttpWebRequest在Windows 8中导致安全异常

时间:2014-08-13 08:29:01

标签: c# winforms windows-8 httpwebrequest

我正在尝试使用HttpWebRequest在运行时将图像加载到Winforms应用程序中的DataGridViewImageColumn中。它在Windows 7和Windows XP上运行良好,但在Windows 8上引发了安全例外。我尝试将config中的信任级别设置为High和Full,但它们都不起作用。任何人都可以建议如何解决这个问题?

//App.config
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<location allowOverride="false">
<system.web>
  <trust level="High"/>
</system.web>
</location>  
</configuration>

string[] Images = { "Apple", "Html5", "Twitter" };
    string imagepath = https://img.domain.com/Image.jpg";
    public Form1()
    {
        try
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = Images;
            dataGridView1.RowTemplate.Height = 120;
        }
        catch (Exception ex)
        {
            System.Diagnostics.EventLog.WriteEntry("WFDGVDemo", ex.StackTrace, System.Diagnostics.EventLogEntryType.Warning);
        }
    }

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        try
        {
            foreach (DataGridViewRow dgvrow in dataGridView1.Rows)
            {
                dgvrow.Cells["Image"].Value = new Bitmap(GetProductImage(imagepath));
                dgvrow.Cells["ImageName"].Value = Images[dgvrow.Index];
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
public Bitmap GetProductImage(string ImagePath)
    {
        try
        {
            Bitmap bmp = null;
            if (!string.IsNullOrEmpty(ImagePath))
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(ImagePath);
                myRequest.Method = "GET";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                bmp = new Bitmap(myResponse.GetResponseStream());
                myResponse.Close();
            }
            return bmp;
        }
        catch (WebException)
        {
            throw;
        }
    }

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个。

  • 打开IIS管理器
  • 双击功能视图中的身份验证图标
  • 启用ASP.Net模拟