connecting to exchange server 2003 from vb.net using webDAV

vasanthya_then

New member
Joined
Sep 4, 2006
Messages
1
Programming Experience
Beginner
Hi All,
I have been trying to connect to Exchange server 2003 from VB.NET.I have tried out some code.But it throws me Login Timeout(440) error.Here is the code:
VB.NET:
Imports System.Net
Imports System.IO
Imports System
Imports System.Xml
Imports System.Text.RegularExpressions
Imports System.Net.HttpWebRequest
Module Module1
Sub Main()
Dim strServerName As String = "servername"
Dim strDomain As String = "domain"
Dim strUserID As String = "userID"
Dim strPassword As String = "password"
' TODO: Replace with the URL of an object on Exchange Server 
Dim sUri As String = "[URL]https://servername/exchange/userID/inbox/[/URL]"
Dim myUri As System.Uri = New System.Uri(sUri)
'Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)
Dim sQuery As String
sQuery = "<?xml version='1.0'?>" & _
"<a:propfind xmlns:a='DAV:'xmlns:m='urn:schemas:mailheader:'>" & _
"<a:prop>" & _
"<m:subject/>" & _
"<a:isfolder/>" & _
"<a:displayname/>" & _
"<a:href/>" & _
"</a:prop>" & _
"</a:propfind>"
' TODO: Replace with appropriate user credential 
Dim myCred As NetworkCredential = New NetworkCredential("domain\userID", "password")
Dim MyCredentialCache As CredentialCache = New CredentialCache()
MyCredentialCache.Add(myUri, "Basic", myCred)
'HttpWRequest.Credentials = MyCredentialCache
' Create our Web request object.
Dim PROPPATCHRequest As HttpWebRequest
PROPPATCHRequest = CType(System.Net.HttpWebRequest.Create(myUri), 
System.Net.HttpWebRequest)
' Authenticate to OWA. Assign the returned cookies to a string.
Dim strReusableCookies As String
strReusableCookies = AuthenticateSecureOWA(strServerName, 
strDomain, strUserID, strPassword)
' Add the cookie set that is obtained after OWA authentication to our request header.
PROPPATCHRequest.Headers.Add("Cookie", strReusableCookies)
PROPPATCHRequest.ContentType = "text/xml"
PROPPATCHRequest.KeepAlive = False
PROPPATCHRequest.AllowAutoRedirect = False
 
'Specify the PROPPATCH method.
PROPPATCHRequest.Method = "GET"
' we need to store the data into a byte array 
Dim ByteQuery() As Byte = System.Text.Encoding.ASCII.GetBytes(sQuery)
PROPPATCHRequest.ContentLength = ByteQuery.Length
Try
Dim QueryStream As Stream = PROPPATCHRequest.GetRequestStream()
' write the data to be posted to the Request Stream 
QueryStream.Write(ByteQuery, 0, ByteQuery.Length)
QueryStream.Close()
Catch ex As Exception
MsgBox("Exception: " + ex.Message)
End Try
' Send Request and Get Response 
PROPPATCHRequest = CType(System.Net.HttpWebRequest.Create(myUri), System.Net.HttpWebRequest)
Dim HttpWResponse As HttpWebResponse = PROPPATCHRequest.GetResponse()
' Get Status and Headers 
Dim iStatCode As Integer = HttpWResponse.StatusCode
Dim sStatus As String = iStatCode.ToString()
Console.WriteLine("Status: {0} {1}", sStatus, HttpWResponse.StatusDescription.ToString())
Console.WriteLine("Request Headers:")
Console.WriteLine(PROPPATCHRequest.Headers.ToString())
Console.WriteLine("Response Headers:")
Console.WriteLine(HttpWResponse.Headers.ToString())
' Get Response Stream 
Dim strm As Stream = HttpWResponse.GetResponseStream()
' Read the Response Steam 
Dim sr As StreamReader = New StreamReader(strm)
Dim sText As String = sr.ReadToEnd()
Console.WriteLine("Response: {0}", sText)
MsgBox("sText: " + sText)
' Close Stream 
strm.Close()
' Clean Up 
PROPPATCHRequest = Nothing
HttpWResponse = Nothing
MyCredentialCache = Nothing
myCred = Nothing
strm = Nothing
sr = Nothing
End Sub
' Code to call the Authentication:
Private CookieJar As CookieContainer
Private strCookies As String
'Implementation of the Authentication to the Exchange Server 2003 server that is 
enabled with forms-based authentication
Private Function AuthenticateSecureOWA(ByVal strServerName As String, 
ByVal strDomain As String, ByVal strUserName As String, ByVal strPassword As String) As String
Dim AuthURL As System.Uri
Try
' Construct our destination URI.
AuthURL = New System.Uri("https://" + strServerName + 
"/exchweb/bin/auth/owaauth.dll")
Catch ex As Exception
MsgBox("Error occurred while you are creating the URI for OWA authentication!" 
+ vbCrLf + vbCrLf + ex.Message)
Return "Error"
End Try
Dim WebReq As HttpWebRequest
CookieJar = New CookieContainer
' Create our request object for the constructed URI.
WebReq = CType(WebRequest.Create(AuthURL), HttpWebRequest)
WebReq.CookieContainer = CookieJar
' Create our post data string that is required by OWA (owaauth.dll).
Dim strPostFields As String = "destination=https%3A%2F%2F" & strServerName &
 "%2Fexchange%2F" + strUserName + "%2F&username=" + strDomain + 
"%5C" + strUserName + "&password=" + strPassword + "&SubmitCreds=
Log+On&forcedownlevel=0&trusted=0"
WebReq.KeepAlive = True
WebReq.AllowAutoRedirect = False
WebReq.Method = "POST"
' Store the post data into a byte array.
Dim PostData() As Byte = System.Text.Encoding.ASCII.GetBytes(strPostFields)
' Set the content length.
WebReq.ContentLength = PostData.Length
Dim tmpStream As Stream
Try
' Create a request stream. Write the post data to the stream.
tmpStream = WebReq.GetRequestStream()
tmpStream.Write(PostData, 0, PostData.Length)
tmpStream.Close()
Catch ex As Exception
MsgBox("Error occurred while trying OWA authentication!" + vbCrLf + vbCrLf + ex.Message)
Return "Error"
End Try
' Get the response from the request.
Dim WebResp As HttpWebResponse = WebReq.GetResponse()
' Create a stream to capture the response data
Dim tmpStreamRead As New StreamReader(WebResp.GetResponseStream())
' Write returned data to a string.
Dim strResponseData As String = tmpStreamRead.ReadToEnd()
tmpStreamRead.Close()
' Close the response object.
WebResp.Close()
' Get our returned cookie set.
strCookies = CookieJar.GetCookieHeader(AuthURL).ToString()
' Filter for our cadata and session ID cookies.
Dim strCADataCookie As String = Regex.Replace(strCookies, "(.*)cadata=""(.*)""(.*)", "$2")
Dim strSessionIDCookie As String = Regex.Replace(strCookies, "(.*)sessionid=(.*)(,|;)(.*)", "$2")
' Create and return the cookie set for performing subsequent Web requests.
strCookies = "sessionid=" + strSessionIDCookie + "; " + "cadata=" + strCADataCookie
Return strCookies
End Function
End Module
----------------------------
I guess the error is bcoz I have not authenticated using SSL certificate which the exchange server (might) expects. Can anyone please help me out?Someone tell me how to use SSL certificates in VB.NET to authenticate to the exchange server 2003.
I've been trying these for several days .
Thanks in advance,
Vasanth.
 
Last edited by a moderator:
Back
Top