D
Dan Marino
Hi,
I have a very strange behaviour of the HttpWebRequest
class.
I have a page that has to send an email. This is an aspx
page that I open from a page of my site in a new window
and in this new window I ask for the emaill address where
to send the email. Then I use the HttpWebRequestClass to
get the content of
another page that I want to put in the email.
The first time that I open the page and try to send the
email it works. Then I close this page and I open it
again and when I try
to send aonther email it stops working and after 60
seconds I get an timeout(I tried increasing the timeout
but to no avail, anyway the first time it takes a second
to send the email)
.. If I kill the aspnet_wp.exe process and I retry it
works the first time but after that it stops working.
Same if I recompile the project it works for the first
time but then it stops working.
It's like this objects stops working after the first call
(or maybe the IIS doesn't deliver the content on
subsequent calls)
I also use in my function a CookieContainer. I need it
because the user is authentified and the page that I call
shows different content
if you are not logged in. If I comment all the code that
refers too the CookieContainer my page works everytime
just that the content that I get is not the one that I
want.
I will put also the content of my function.
Anybody having any idea why this behaviour and if it's a
bug in VB.NET can I send an email from and aspx page in
another manner?
Thanks,
Dan Marino
Dim myRequest As System.Net.HttpWebRequest
Dim myResponse As System.Net.WebResponse
Dim oStream As System.IO.Stream
Dim oStreamReader As System.IO.StreamReader
Dim WebAddressToCall As String
Dim EmailContent As String = ""
Dim RndInt As Int32 = CInt(100000 * Rnd())
Try
WebAddressToCall = Request.Url.AbsoluteUri.Substring
(0, Request.Url.AbsoluteUri.LastIndexOf("/"))
& "/PrivateViewDocumentEmail.aspx?id=" &
Request.QueryString.Item("id").ToString
myRequest = CType(WebRequest.Create
(WebAddressToCall), HttpWebRequest)
myRequest.CookieContainer = New CookieContainer(100)
Dim CookieCounter As Integer = 0
Dim oTmpCookie As New Cookie()
While CookieCounter < Request.Cookies.Count
oTmpCookie.Name = Request.Cookies.Item
(CookieCounter).Name
oTmpCookie.Value = Request.Cookies.Item
(CookieCounter).Value
'oTmpCookie.Path = Request.Cookies.Item
(CookieCounter).Path
oTmpCookie.Domain = CStr(IIf(Request.Cookies.Item
(CookieCounter).Domain Is Nothing,
Request.ServerVariables.Item("HTTP_HOST").ToString,
Request.Cookies.Item(CookieCounter).Domain))
myRequest.CookieContainer.Add(oTmpCookie)
CookieCounter += 1
End While
myRequest.Credentials =
CredentialCache.DefaultCredentials
myResponse = myRequest.GetResponse()
oStream = myResponse.GetResponseStream()
oStreamReader = New System.IO.StreamReader(oStream)
EmailContent = oStreamReader.ReadToEnd
oStreamReader.Close()
oStream.Close()
myResponse.Close()
Return EmailContent
Catch ex As System.Exception
Throw ex
Finally
If Not IsNothing(myRequest) Then
myRequest.CookieContainer = Nothing
myRequest = Nothing
End If
If Not IsNothing(myResponse) Then
myResponse = Nothing
End If
If Not IsNothing(oStream) Then
oStream = Nothing
End If
If Not IsNothing(oStreamReader) Then
oStreamReader = Nothing
End If
End Try
I have a very strange behaviour of the HttpWebRequest
class.
I have a page that has to send an email. This is an aspx
page that I open from a page of my site in a new window
and in this new window I ask for the emaill address where
to send the email. Then I use the HttpWebRequestClass to
get the content of
another page that I want to put in the email.
The first time that I open the page and try to send the
email it works. Then I close this page and I open it
again and when I try
to send aonther email it stops working and after 60
seconds I get an timeout(I tried increasing the timeout
but to no avail, anyway the first time it takes a second
to send the email)
.. If I kill the aspnet_wp.exe process and I retry it
works the first time but after that it stops working.
Same if I recompile the project it works for the first
time but then it stops working.
It's like this objects stops working after the first call
(or maybe the IIS doesn't deliver the content on
subsequent calls)
I also use in my function a CookieContainer. I need it
because the user is authentified and the page that I call
shows different content
if you are not logged in. If I comment all the code that
refers too the CookieContainer my page works everytime
just that the content that I get is not the one that I
want.
I will put also the content of my function.
Anybody having any idea why this behaviour and if it's a
bug in VB.NET can I send an email from and aspx page in
another manner?
Thanks,
Dan Marino
Dim myRequest As System.Net.HttpWebRequest
Dim myResponse As System.Net.WebResponse
Dim oStream As System.IO.Stream
Dim oStreamReader As System.IO.StreamReader
Dim WebAddressToCall As String
Dim EmailContent As String = ""
Dim RndInt As Int32 = CInt(100000 * Rnd())
Try
WebAddressToCall = Request.Url.AbsoluteUri.Substring
(0, Request.Url.AbsoluteUri.LastIndexOf("/"))
& "/PrivateViewDocumentEmail.aspx?id=" &
Request.QueryString.Item("id").ToString
myRequest = CType(WebRequest.Create
(WebAddressToCall), HttpWebRequest)
myRequest.CookieContainer = New CookieContainer(100)
Dim CookieCounter As Integer = 0
Dim oTmpCookie As New Cookie()
While CookieCounter < Request.Cookies.Count
oTmpCookie.Name = Request.Cookies.Item
(CookieCounter).Name
oTmpCookie.Value = Request.Cookies.Item
(CookieCounter).Value
'oTmpCookie.Path = Request.Cookies.Item
(CookieCounter).Path
oTmpCookie.Domain = CStr(IIf(Request.Cookies.Item
(CookieCounter).Domain Is Nothing,
Request.ServerVariables.Item("HTTP_HOST").ToString,
Request.Cookies.Item(CookieCounter).Domain))
myRequest.CookieContainer.Add(oTmpCookie)
CookieCounter += 1
End While
myRequest.Credentials =
CredentialCache.DefaultCredentials
myResponse = myRequest.GetResponse()
oStream = myResponse.GetResponseStream()
oStreamReader = New System.IO.StreamReader(oStream)
EmailContent = oStreamReader.ReadToEnd
oStreamReader.Close()
oStream.Close()
myResponse.Close()
Return EmailContent
Catch ex As System.Exception
Throw ex
Finally
If Not IsNothing(myRequest) Then
myRequest.CookieContainer = Nothing
myRequest = Nothing
End If
If Not IsNothing(myResponse) Then
myResponse = Nothing
End If
If Not IsNothing(oStream) Then
oStream = Nothing
End If
If Not IsNothing(oStreamReader) Then
oStreamReader = Nothing
End If
End Try