By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,696 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,696 IT Pros & Developers. It's quick & easy.

HttpWebRequest problem-please help

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Dan,

I don't know the answer to your problem, but a quick glance shows me that:

1)
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

Can be replaced by

myRequest.CookieContainer = Nothing
myRequest = Nothing
myResponse = Nothing
oStream = Nothing
oStreamReader = Nothing

2)
oStreamReader.Close()
oStream.Close()
myResponse.Close()

These won't be executed if there's an Exception. They should be put in
your Finally section

3)
Return EmailContent

This will prevent your Finally from being executed in the non-Exception
case. It should be put after the Finally.

4)
Dim CookieCounter As Integer = 0
While CookieCounter < Request.Cookies.Count
CookieCounter += 1
End While

You might as well use a For loop for this.

5)
You say that removing the Cookie code eliminates the problem. Have you
tried removing it piecemeal as well as in a block. Eg, if you remove the
CookieContainer.Add, does this make the problem go away? If you replace one or
more of the Request.Cookies.Item() or Request.ServerVariables.Item() does that
make a difference? Does the size (100) make a difference?

Good luck. :-)

Regards,
Fergus
Nov 20 '05 #2

P: n/a
Cor
Hi Dan,

If it works without the cookie part than I think too that it have to be in
the in that code.
I just said Duran that I would maybe look for vb.net language code about a
cookie and now you did sent it.

Now it is champion league time here, but it intrest me so maybe I look
tonight.

When tell you I go look for it, I don't say I find it.

If you find it yourself, will you tell it please.
Cor
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.