利用AD域访问web资源

来源:互联网 发布:mac导入照片不清晰 编辑:程序博客网 时间:2024/05/21 12:45

利用 WebClient :

WebClient wc = new WebClient();WebClient.UseDefaultCredentials = true;

它会直接在 HTTP Header 中加 www-authenticate 头标识。HttpWebRequest 也是一样。

NTML 是一种在网络上运行 Microsoft Windows 的系统之间的基于 Challenge 的验证机制。
详细请参考 http://msdn.microsoft.com/en-us/library/aa378749(VS.85).aspx。

private void ReadResource(string url){    WebClient wc = new WebClient();    WebClient.UseDefaultCredentials = true;    string uri = url;    Stream stream = wc.OpenRead(uri);    StreamReader sr = new StreamReader(stream);    string strLine = "";    while ((strLine = sr.ReadLine()) != null)    {      this.listBox1.Items.Add(strLine);    }    sr.Close(); }

向服务器写入

private void Write(url){    WebClient wc = new WebClient();    string uri = url;    Stream stream = wc.OpenWrite(uri, "PUT");    StreamWriter sw = new StreamWriter(stream);        sw.WriteLine("HelloWorldHelloWorldHelloWorldHelloWorld");    sw.Flush();    sw.Close(); }