远程服务器返回错误:(403)禁止

use*_*963 24 c#

我已经编写了一个可以运行数月的应用程序,在过去的几天里我只在安装的版本上收到了以下错误.

如果我在VS中运行源代码一切正常.此外,bin文件夹中的.exe工作正常.它只是生成错误的已安装版本,如果我重新编译并重新安装,我会得到相同的错误.

关于造成这种情况的原因我有点难过,希望能指点一下.它似乎是通过IE的WebRequest响应没有返回,但我很难过为什么它在VS中正常工作没有任何错误.是否有任何新的IE安全措施/政策可能导致此问题?

到目前为止我尝试过的事情包括:

  • 禁用所有AntiVirus和防火墙
  • 以管理员身份运行

例外情况:

Exception: System.Windows.Markup.XamlParseException: The invocation of the constructor on type 'XApp.MainWindow' that matches the specified binding constraints threw an exception. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden.
   at System.Net.HttpWebRequest.GetResponse()
   at XApp.HtmlRequest.getHtml(Uri uri) in J:\Path\MainWindow.xaml.cs:line 3759
   at XApp.MainWindow.GetLinks() in J:\Path\MainWindow.xaml.cs:line 2454
   at XApp.MainWindow..ctor() in J:\Path\MainWindow.xaml.cs:line 124
   --- End of inner exception stack trace ---
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
   at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
   at System.Windows.Application.DoStartup()
   at System.Windows.Application.<.ctor>b__1(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
Exception: System.Net.WebException: The remote server returned an error: (403) Forbidden.
   at System.Net.HttpWebRequest.GetResponse()
   at XApp.HtmlRequest.getHtml(Uri uri) in J:\Path\MainWindow.xaml.cs:line 3759
   at XApp.MainWindow.GetLinks() in J:\Path\MainWindow.xaml.cs:line 2454
   at XApp.MainWindow..ctor() in J:\Path\MainWindow.xaml.cs:line 124
Run Code Online (Sandbox Code Playgroud)

编辑:

这是作为独立应用程序安装的.当我以管理员身份运行时,我打开了程序文件夹并以管理员身份运行exe而不是快捷方式.

导致问题的代码是这样的

private void GetLinks()
{
    //Navigate to front page to Set cookies
    HtmlRequest htmlReq = new HtmlRequest();

    OLinks = new Dictionary<string, List<string>>();

    string Url = "http://www.somesite.com/somepage";
    CookieContainer cookieJar = new CookieContainer();
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
    request.CookieContainer = cookieJar;

    request.Accept = @"text/html, application/xhtml+xml, */*";
    request.Referer = @"http://www.somesite.com/";
    request.Headers.Add("Accept-Language", "en-GB");
    request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)";
    request.Host = @"www.somesite.com";

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    String htmlString;
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
        htmlString = reader.ReadToEnd();
    }

    //More Code


 }
Run Code Online (Sandbox Code Playgroud)

Bud*_*dda 17

看起来问题是基于服务器端.

我是我的情况我使用贝宝服务器,并没有建议的答案帮助,但http://forums.iis.net/t/1217360.aspx?HTTP+403+禁止+错误

我正面临这个问题,刚收到Paypal技术支持的回复.添加此项将修复403问题. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.UserAgent = "[any words that is more than 5 characters]";

  • 这是我在尝试使用 GitHub API 时遇到的“Forbidden”问题的解决方案。我花了几个小时的搜索才偶然发现了这篇文章。 (2认同)

x0n*_*x0n 16

添加以下行:

request.UseDefaultCredentials = true;
Run Code Online (Sandbox Code Playgroud)

这将允许应用程序使用登录用户的凭据来访问该站点.如果它返回403,显然它需要认证.

您(现在?)也可能在您和远程站点之间拥有身份验证代理.在这种情况下,尝试:

request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.