2013-01-09 33 views
0

我的代码是这样的HttpWebRequest的不及格证书简单形式的认证

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(); 

我用我的网站简单形式的验证,我需要以打印通过abcPDF一个PDF验证这个WebRequest的,如我的登录细节存储在cookie中,我试图从那里获取并添加到我的请求。我认为错误是本着request.Credentials = new NetworkCredential("username", "password");

回答

2

你说的网站使用窗体身份验证,但是你的要求是使用基本身份验证凭据

request.Credentials = new NetworkCredential("username", "password"); 

你要么需要切换网站到基本身份验证,或对您的登录页面执行POST请求以获取会话cookie /令牌以用于后续请求。

+0

你能给我一个针对登录页面的POST请求的代码示例,以及我需要如何将它与我的代码集成吗? – Athul