感谢您使用微软产品。要使用C#对Exchange Server 2000进行搜索,你需要使用System.Net namespace下的 HttpWebRequest类来发出请求,HttpWebResponse类来接受回应,另外你需要参考Exhange Server2000的接口文档。请参照下面的例子:using System;
using System.Net;
using System.IO; namespace WebDavNET
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
try
{
string sUri = "http://ExchServer/Exchange/UserAlias/Inbox/"; //换成你的Exhange Server 的URI System.Uri myUri = new System.Uri(sUri);
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri); string sQuery;
sQuery = "<?xml version='1.0'?>" +
"<g:searchrequest xmlns:g='DAV:'>" +
"<g:sql>SELECT \"DAV:displayname\" " +
"FROM SCOPE('SHALLOW TRAVERSAL OF \"" + sUrl + "\"') " +
"WHERE \"DAV:isfolder\" = false" +
"</g:sql>" +
"</g:searchrequest>"; // Set credentials
NetworkCredential myCred = new NetworkCredential(@"DomainName\UserName", "UserPassword");
CredentialCache MyCrendentialCache = new CredentialCache();
MyCrendentialCache.Add(myUri, "Basic", myCred);
HttpWRequest.Credentials = MyCrendentialCache; // Set some headers
HttpWRequest.UserAgent = "CSharp HTTP Sample";
HttpWRequest.KeepAlive = true; //this is the default
HttpWRequest.Headers.Set("Pragma", "no-cache"); HttpWRequest.Headers.Set("Translate", "f");
HttpWRequest.Headers.Set("Depth", "0");
HttpWRequest.ContentType = "text/xml";
HttpWRequest.ContentLength = sQuery.Length; //set the request timeout to 5 min.
HttpWRequest.Timeout = 300000;
// set the request method
HttpWRequest.Method = "SEARCH"; // we need to store the data into a byte array
byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery);
HttpWRequest.ContentLength = ByteQuery.Length;
Stream QueryStream = HttpWRequest.GetRequestStream();
// write the data to be posted to the Request Stream
QueryStream.Write(ByteQuery,0,ByteQuery.Length);
QueryStream.Close(); // Send Request and Get Response
HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse(); // Get the Status code
int iStatCode = (int)HttpWResponse.StatusCode;
string sStatus = iStatCode.ToString();
// Get the status text
string sStatusCode = HttpWResponse.StatusCode.ToString();
// Get the request headers
string sReqHeaders = HttpWRequest.Headers.ToString(); // Read Response Stream
Stream strm = HttpWResponse.GetResponseStream();
StreamReader sr = new StreamReader(strm);
string sText = sr.ReadToEnd();
Console.WriteLine(sText); // Close Stream
strm.Close(); // Clean up
HttpWRequest = null;
HttpWResponse = null;
strm = null;
sr = null;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
}
更详细的信息,请参阅MSDN中的HttpWebRequest/HttpWebResponse类:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebRequestClassTopic.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebResponseClassTopic.asp和Exhange Server 的相关文档:
http://www.microsoft.com/exchange/techinfo/default.asp======================
- 微软全球技术中心 微软全球技术中心 VC技术支持本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================
using System.Net;
using System.IO; namespace WebDavNET
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
try
{
string sUri = "http://ExchServer/Exchange/UserAlias/Inbox/"; //换成你的Exhange Server 的URI System.Uri myUri = new System.Uri(sUri);
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri); string sQuery;
sQuery = "<?xml version='1.0'?>" +
"<g:searchrequest xmlns:g='DAV:'>" +
"<g:sql>SELECT \"DAV:displayname\" " +
"FROM SCOPE('SHALLOW TRAVERSAL OF \"" + sUrl + "\"') " +
"WHERE \"DAV:isfolder\" = false" +
"</g:sql>" +
"</g:searchrequest>"; // Set credentials
NetworkCredential myCred = new NetworkCredential(@"DomainName\UserName", "UserPassword");
CredentialCache MyCrendentialCache = new CredentialCache();
MyCrendentialCache.Add(myUri, "Basic", myCred);
HttpWRequest.Credentials = MyCrendentialCache; // Set some headers
HttpWRequest.UserAgent = "CSharp HTTP Sample";
HttpWRequest.KeepAlive = true; //this is the default
HttpWRequest.Headers.Set("Pragma", "no-cache"); HttpWRequest.Headers.Set("Translate", "f");
HttpWRequest.Headers.Set("Depth", "0");
HttpWRequest.ContentType = "text/xml";
HttpWRequest.ContentLength = sQuery.Length; //set the request timeout to 5 min.
HttpWRequest.Timeout = 300000;
// set the request method
HttpWRequest.Method = "SEARCH"; // we need to store the data into a byte array
byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery);
HttpWRequest.ContentLength = ByteQuery.Length;
Stream QueryStream = HttpWRequest.GetRequestStream();
// write the data to be posted to the Request Stream
QueryStream.Write(ByteQuery,0,ByteQuery.Length);
QueryStream.Close(); // Send Request and Get Response
HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse(); // Get the Status code
int iStatCode = (int)HttpWResponse.StatusCode;
string sStatus = iStatCode.ToString();
// Get the status text
string sStatusCode = HttpWResponse.StatusCode.ToString();
// Get the request headers
string sReqHeaders = HttpWRequest.Headers.ToString(); // Read Response Stream
Stream strm = HttpWResponse.GetResponseStream();
StreamReader sr = new StreamReader(strm);
string sText = sr.ReadToEnd();
Console.WriteLine(sText); // Close Stream
strm.Close(); // Clean up
HttpWRequest = null;
HttpWResponse = null;
strm = null;
sr = null;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
}
更详细的信息,请参阅MSDN中的HttpWebRequest/HttpWebResponse类:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebRequestClassTopic.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebResponseClassTopic.asp和Exhange Server 的相关文档:
http://www.microsoft.com/exchange/techinfo/default.asp======================
- 微软全球技术中心 微软全球技术中心 VC技术支持本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货