How to request a permission of type System.Net.Web

2020-04-07 04:34发布

I created a small web service that creates a HttpWebRequest to another website using a proxy and after i used the proxies i got that error:

Request for the permission of type 'System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

I have no idea about how to fix it.

Here is my code,

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://website.com");
req.CookieContainer = c;

req.Proxy = new WebProxy("IP:PORT");

req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB7.4; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2)";
req.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
req.Headers.Add("Accept-Language", "en-US");
req.ServicePoint.Expect100Continue = false;
req.AllowAutoRedirect = false;
req.Timeout = 10000;
req.Method = "GET";
req.KeepAlive = true;
req.ProtocolVersion = HttpVersion.Version10;

3条回答
Lonely孤独者°
2楼-- · 2020-04-07 04:46

I was also having this issue - and it turned out to be due to dll performing the web request having being downloaded from the web.

I had to go into the Properties of the dll in file explorer (right click> Properties) and click "Unblock" - making sure to click "Apply" and then "Ok".

查看更多
Animai°情兽
3楼-- · 2020-04-07 04:53

It seems that you are running into a trust related issue. You might be able to work-around the issue by including in your Web.Config, under System.Web,

<trust level="Full" />

WebPermission is not available under a medium trust environment (and you can look at this article to modify this behavior and for the reasons why you shouldn't).

However, if you require full trust you might have to reconsider the requirements of your application.

查看更多
迷人小祖宗
4楼-- · 2020-04-07 05:10

go to properties of your Project/application (WPF)

In security select enable clickone securitysetting. and set trust level of the project

查看更多
登录 后发表回答