我在使用表单身份验证的网站上遇到了类似的问题,我能够通过使用此处提供的代码作为线程中的第二个回复来解决此问题.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); // Add the current authentication cookie to the request HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; Cookie authenticationCookie = new Cookie( FormsAuthentication.FormsCookieName, cookie.Value, cookie.Path, HttpContext.Current.Request.Url.Authority); req.CookieContainer = new CookieContainer(); req.CookieContainer.Add(authenticationCookie); WebResponse res = req.GetResponse();
我想你想要这个:
req.Credentials = CredentialCache.DefaultCredentials;