Re: Close OWA Connection
- From: NgelW <NgelW@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 3 May 2006 09:45:03 -0700
Hi,
After connecting to exchange, I collect the cookies as such:
----- code snippet -----
try
{
//Get server portion of uri and append Exchange auth dll
server = uri.Substring(0, uri.IndexOf("/", 8)) +
"/exchweb/bin/auth/owaauth.dll";
//Build request
request = GetRequestObject(server, "POST");
request.CookieContainer = new CookieContainer();
request.ContentType = "application/x-www-form-urlencoded";
//Prepare body of request
body =
Encoding.UTF8.GetBytes(string.Format("destination={0}&username={1}\\{2}&password={3}",
uri, credential.Domain, credential.UserName, credential.Password));
request.ContentLength = body.Length;
//Send request to server
stream = request.GetRequestStream();
stream.Write(body, 0, body.Length);
stream.Close();
//Get response
response = (HttpWebResponse)request.GetResponse();
//Check if login successful
if (response.Cookies.Count < 2) throw
new Exception("Failed to login to OWA. Be sure your domain and password are
correct.");
//Build OverLoaded Cookie Collection
CookieCollection responseCookies = new CookieCollection();
//Add cookies to this collection
responseCookies.Add(response.Cookies[0]);
responseCookies.Add(response.Cookies[1]);
//Return the cookie collection
return responseCookies;
}
----- code snippet -----
I then add these cookies into any further requests, which seems to work
fine, for a period of time. Then I start getting Timeouts. I assume this is
because my exchange session as timed-out. To get around this, I wanted to
close my exchange session (i.e logoff) after every connection I make. I
tried the code you provided, but no luck. Would I not have to include the
cookies in the HTTP request containing the ?cmd=logoff command, or is the
credential set enough info for exchange? ((that wouldn't seem very secure))
Is this the appropriate way to maintain a connection to exchange - I would
either like the timeout period on the exchange session renewed upon every
action (and a re-login when it timesout with inactivity), or preferably, the
session to be opened and closed on every request (hence never have a timeout
problem).
Also, is there a reference to what commands like \?Cmd=logoff are availabkle
with exchange server?? i have the Exchange 2003 SDK installed - but there
doesn't seem to be a whole lot in it....
I probably should mention that this is for a sharepoint (wss) web part.
Many thanks,
Nigel W
"Glen Scales [MVP]" wrote:
That error sounds like your trying to send some sort of body with you get.
requestion which you shouldn't be doing the following code snippet seems to
work okay for me
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
namespace ExchangeSDK.Snippets.CSharp
{
class GettingItemPropertyValuesWebDAV
{
[STAThread]
static void Main(string[] args)
{
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strSrcURI = "http://server/exchange/mailbox/?Cmd=logoff";
string strUserName = "user";
string strPassword = "password";
string strDomain = "domain";
System.IO.Stream ResponseStream = null;
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add( new System.Uri(strSrcURI),
"Basic",
new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
);
Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strSrcURI);
Request.Credentials = MyCredentialCache;
Request.Method = "GET";
Request.ContentType = "text/xml";
Response = (HttpWebResponse)Request.GetResponse();
ResponseStream = Response.GetResponseStream();
StreamReader objReader = new StreamReader(ResponseStream);
string sLine = "";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
Console.WriteLine("{0}:{1}",i,sLine);
}
Console.ReadLine();
ResponseStream.Close();
Response.Close();
}
}
}
Cheers
Glen
"NgelW" <NgelW@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B4B7BC33-B4DD-43DA-8C69-1BDE070517AA@xxxxxxxxxxxxxxxx
Thanks - I have tried to implement this as a GET call (hacked from a
function
performing a POST call), but it returns a "Cannot send a content-body with
this verb-type" error <see code snippet below>
Is there (yet again) something obvious that I am doing wrong? Also, it
suprises me a little that I don't need to use the cookies gained when I
established my connection to OWA to terminate the connection?!?
----- code snippet -----
public void BreakAuthentication(string uri)
{
//Paramters
HttpWebRequest request;
HttpWebResponse response;
byte[] body;
Stream stream;
string uriToUse;
//Update URI
uriToUse = uri.Substring(0, uri.IndexOf("/", 8)) + "/?Cmd=logoff";
//Build request
request = GetRequestObject(uriToUse, "GET");
//This is probably the problem?!? What sort of Content-Type should I
//use?
request.ContentType = "application/x-www-form-urlencoded";
//Send request to server
stream = request.GetRequestStream();
stream.Close();
//Get response
response = (HttpWebResponse)request.GetResponse();
}
---- end code snippet ----
"Glen Scales [MVP]" wrote:
Are you using Exchange 2003 is so you could just make a call to the OWA
Logoff command eg
GET /exchange/user/?Cmd=logoff
cheers
Glen
"NgelW" <NgelW@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:77572BDF-93BC-4C03-B904-264774EEE3DE@xxxxxxxxxxxxxxxx
Hi,
In a c# app, I have established a connection to OWA, and done what I
need
to
do on the exchange server, and would now like to close that connection
rather
then allowing it to time-out. Can anyone explain how this can be done?
Many thanks,
Nigel
- Follow-Ups:
- Re: Close OWA Connection
- From: Glen Scales [MVP]
- Re: Close OWA Connection
- Prev by Date: How to find deleted Contacts and appointments & task folder properties
- Next by Date: Re: How to find deleted Contacts and appointments & task folder properties
- Previous by thread: How to find deleted Contacts and appointments & task folder properties
- Next by thread: Re: Close OWA Connection
- Index(es):
Relevant Pages
|