数ヶ月間正常に動作していたアプリを書いたのですが、ここ数日、インストールしたバージョンに限って以下のエラーが発生します。
VSでソースコードを実行すると、すべて正常に動作します。また、binフォルダーにある.exeも問題なく動作します。インストールしたバージョンのみがエラーを発生させ、再コンパイルして再インストールすると、同じエラーが発生します。
何が原因なのか、ちょっと困っています。IE経由のWebRequestのレスポンスが返ってこないようなのですが、なぜVSでは何のエラーもなくうまくいくのか、困っています。IEの新しいセキュリティ対策やポリシーが原因なのでしょうか?
今までに試したことは以下の通りです。
例外が発生しました。
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
EDIT
これはスタンドアロンアプリとしてインストールされています。Administratorで実行した場合、ショートカットではなく、プログラムフォルダを開いてexeをAdministratorで実行した場合です。
問題の原因となるコードは次の通りです。
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
}
次の行を追加します。
request.UseDefaultCredentials = true;
これにより、アプリケーションはサイトにアクセスするためにログインしたユーザーの認証情報を使用するようになります。もし403が返ってくるなら、明らかに認証を期待しています。
また、リモートサイトとの間に認証用プロキシを設置している可能性もあります(現在は?その場合、試してみてください。
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);
<br>.Paypal.WebRequest = (HttpWebRequest)WebRequest.Create(url);<br>
req.UserAgent = "[5文字以上の任意の単語]";`を追加してください。
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<googleshortenedurlresponse>(googReturnedJson);
//ViewBag.ShortenedUrl = googUrl.id;
return View();
}
これはおそらくあまり多くの人を助けることにはなりませんが、これは私のケースでした。私はJira Rest Apiを使用していて、私の個人的な資格情報(Jiraへのログインに使用するもの)を使用していました。 Jiraパスワードを更新しましたが、コードで更新するのを忘れていました。 403エラーを取得しました。コードでパスワードを更新してみましたが、それでもエラーが発生しました。
解決策:(ログインページから)Jiraにログインしてみたところ、ボットではないことを証明するためにテキストを入力する必要がありました。 その後、コードからもう一度試したところ、うまくいきました。 要点:サーバーがあなたをロックアウトした可能性があります。