Wednesday, 21 August 2013

HttpWebRequest.Method = "Search" Causes Login Timeout

HttpWebRequest.Method = "Search" Causes Login Timeout

I've been editing this question over and over as I've traversed these
errors, but I think I've been able to target the crucial issue.
I've been trying to connect to Exchange 2003 to get some information out
of the calendar. Originally, I thought the problem was that I couldn't
connect at all. The errors differed depending on which settings I used,
but I could never establish the connection. As it turns out, I can
connect--but when I use request.Method = "SEARCH" I consistently get a
(440) Login Timeout.
I don't understand. You'll see that I have another line commented out,
where I use Post as the request method. When I use that, it connects. It
doesn't bring back the data that I want (which is why the xml.Load is
commented out), but it connects to the server and returns something--a lot
of question marks.
But every example I can find online that does what I am trying to do uses
request.Method = "SEARCH". In particular, at MSDN, CodeProject, and this
blog post.
Maybe there's an element I'm missing that enables the "SEARCH" method, but
Ive tried a lot of variations. I've narrowed down the code below to the
most basic pieces.
I should probably mention that I'm trying to connect to a series of
mailboxes using a service account. I'm not sure what difference that would
make. I know the service account has all necessary permissions to access
these folders and edit them.
If there are any other diagnostics I can do, or any particular data I can
try to get back to test this connection, I'm open to suggestion. This is
my first time working with Exchange.
static void Main(string[] args)
{
string user = "user";
string password = "password";
string domain = "mysite.com";
string baseURI = @"http://mail.myserver.net/";
string mailbox = "exchange/t01";
string calendarURI = baseURI + mailbox + "/calendar";
string strQuery;
byte[] bytes = null;
strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D =
\"DAV:\" >";
strQuery += "<D:sql>SELECT \"DAV:href\" FROM scope('hierarchical
traversal of \"";
strQuery += calendarURI + "\"')</D:sql></D:searchrequest>";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseURI
+ mailbox + "/calendar");
request.Credentials = new NetworkCredential(user, password, domain);
//request.Method = WebRequestMethods.Http.Post;
request.Method = "SEARCH";
bytes = Encoding.UTF8.GetBytes(strQuery);
Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
request.ContentType = "text/xml";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
XmlDocument xml = new XmlDocument();
//xml.Load(responseStream);
responseStream.Close();
Console.WriteLine("Successful login");
Console.ReadLine();
}

No comments:

Post a Comment