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

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

如果我在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.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 

编辑:

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

导致问题的代码是这样的

 private void GetLinks() { //Navigate to front page to Set cookies HtmlRequest htmlReq = new HtmlRequest(); OLinks = new Dictionary<string, List>(); 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 } 

添加以下行:

 request.UseDefaultCredentials = true; 

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

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

 request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; 

希望这可以帮助。

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

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

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

它的权限错误。 您的VS可能使用提升的帐户或不同的用户帐户运行,而不是使用已安装版本的用户。

检查IIS权限并查看哪些帐户可以访问您正在访问的资源可能很有用。 与您使用的帐户以及安装的版本正在使用的帐户交叉引用。

  private class GoogleShortenedURLResponse { public string id { get; set; } public string kind { get; set; } public string longUrl { get; set; } } private class GoogleShortenedURLRequest { public string longUrl { get; set; } } public ActionResult Index1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult ShortenURL(string longurl) { string googReturnedJson = string.Empty; JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer(); GoogleShortenedURLRequest googSentJson = new GoogleShortenedURLRequest(); googSentJson.longUrl = longurl; string jsonData = javascriptSerializer.Serialize(googSentJson); byte[] bytebuffer = Encoding.UTF8.GetBytes(jsonData); WebRequest webreq = WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url"); webreq.Method = WebRequestMethods.Http.Post; webreq.ContentLength = bytebuffer.Length; webreq.ContentType = "application/json"; using (Stream stream = webreq.GetRequestStream()) { stream.Write(bytebuffer, 0, bytebuffer.Length); stream.Close(); } using (HttpWebResponse webresp = (HttpWebResponse)webreq.GetResponse()) { using (Stream dataStream = webresp.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStream)) { googReturnedJson = reader.ReadToEnd(); } } } //GoogleShortenedURLResponse googUrl = javascriptSerializer.Deserialize(googReturnedJson); //ViewBag.ShortenedUrl = googUrl.id; return View(); } 

这可能不会帮助太多人,但这是我的情况:我正在使用Jira Rest Api并使用我的个人凭据(我用来登录Jira的凭据)。 我更新了我的Jira密码,但忘了在我的代码中更新它们。 我收到403错误,我尝试在代码中更新我的密码,但仍然收到错误。

解决方案:我尝试登录Jira(从他们的登录页面),我必须输入文本以certificate我不是机器人。 之后,我再次尝试从代码中运行。 外卖:服务器可能已将您锁定。

在我的情况下,我记得前一段时间为这个地址创建了防火墙漏洞,所以我不得不在配置文件中的绑定上设置useDefaultWebProxy =“false”,好像默认是使用代理,如果useDefaultWebProxy是未标明。

设置:

 request.Referer = @"http://www.somesite.com/"; 

并添加了比我工作的cookies

我们应该使用证书中给出的名称访问该网站。 在此处输入图像描述