我需要以编程方式从SharePoint服务器下载文件。
当我使用Firefox下载文件时,它看起来像是一个请求,但Httpfox显示HTTPS对话实际上是4个请求:
REQ1: GET https://mycorp.raxsp.com/_windows/default.aspx?ReturnUrl=/personal/mycorp_user1/_vti_bin/cmis/rest?getRepositories
RESP1: 401 Unauthorized, WWW-Authenticate NTLM
REQ2: Authorization NTLM TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=
RESP2: 401, WWW-Authenticate NTLM TlRMTVNTUAACAAAACgAKADgAAAAFgokC+[...]
REQ3: Authorization NTLM TlRMTVNTUAADAAAAGAAYAIAAAAAYA[...]
RESP3: 302 Found, Set-Cookie FedAuth=77u/PD94bW[...], Location /personal/mycorp_user1/_vti_bin/cmis/rest?getRepositories
REQ4: GET /personal/mycorp_user1/_vti_bin/cmis/rest?getRepositories
RESP4: 200 OK, <download begins>
我尝试使用带有用户/密码的简单HttpWebRequest下载文件,但正如预期的那样我只得到错误401.我正在考虑实施全部4个请求,使用NTLM over HTTP authentication algorithm计算挑战( spec),但这听起来很容易出错...
是否有客户端库或通过HTTP身份验证执行NTLM的代码段? 它适用于an Open Source project,因此必须是开源,最好使用HttpWebRequest 没有涉及Kerberos / SSO /域。
答案 0 :(得分:1)
我们使用System.Net.WebClient
public static byte [] downloadSharepointFile (string url){
using (var client = new WebClient { Credentials = new NetworkCredential("username", "password", "domain") })
{
client.Headers.Add("Accept: application/json");
return client.DownloadData(url);
}
}