我的代码就像这样
Doc doc = new Doc();
string url = HttpContext.Current.Request.Url.AbsoluteUri;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// required for HttpWebResponse.Cookies
request.CookieContainer = new CookieContainer();
request.Credentials = new NetworkCredential("username", "password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.Cookies.Count>0) { // includes ASP.NET_SessionId
bool needsCookie2 = false;
StringBuilder builder = new StringBuilder("Cookie: ");
for(int i = 0; i<response.Cookies.Count; ++i) {
Cookie cookie = response.Cookies[i];
if(!needsCookie2 && cookie.Version!=1)
needsCookie2 = true;
if(i>0)
builder.Append("; ");
builder.Append(cookie.ToString());
}
builder.Append(!needsCookie2? "\r\n": "\r\nCookie2: $Version=1\r\n");
doc.HtmlOptions.HttpAdditionalHeaders = builder.ToString();
}
doc.HtmlOptions.NoCookie = true;
doc.HtmlOptions.HostWebBrowser = false;
// cookieless Forms Authentication adds authentication ticket to the URL
int id = doc.AddImageUrl(response.ResponseUri.AbsoluteUri);
doc.Save(HttpContext.Current.Server.MapPath("~/media/pdf/1212.pdf"));
doc.Clear();
我在我的网站中使用简单表单身份验证,我需要验证此webRequest以通过abcPDF打印pdf,因为我的登录详细信息存储在cookie中我试图从中获取它在那里并添加到我的请求。我假设错误在行request.Credentials = new NetworkCredential("username", "password");
答案 0 :(得分:2)
您说该网站正在使用表单身份验证,但您的请求是使用基本身份验证凭据:
request.Credentials = new NetworkCredential("username", "password");
您需要将网站切换到基本身份验证,或者对登录页面执行POST请求,以便在后续请求中使用会话cookie /令牌。