473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

HttpWebRequest problem-please help

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
2 3305
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dan Battagin | last post by:
Is there a known bug with the interaction between the HttpWebRequest and the ThreadPool? I current spawn several HttpWebRequest's using BeginGetResponse, and they work for a while, using worker...
2
by: Kueishiong Tu | last post by:
I have a url, I pass it to Webclient, and I get response without any problem. String* uriString = S"trade7.masterlink.com.tw/futures/QuotePrice.jsp"; String* postData = S""; // Create a new...
6
by: omyek | last post by:
I'm trying to mimic the browsing of a webpage using an HttpWebRequest. I've had a lot of luck with it so far, including logging into pages, posting form data, and even collecting and using cookies....
16
by: thomas peter | last post by:
I am building a precache engine... one that request over 100 pages on an remote server to cache them remotely... can i use the HttpWebRequest and WebResponse classes for this? or must i use the...
4
by: R Reyes | last post by:
I am trying to code a file uploader (for forum/email attachments) from the client computer to a remote web server via the PUT method (since POST is not allowed ). However, the upload works ONLY...
7
by: gorkos | last post by:
Hi, I am two days trying to solve a problem with some pages, which i get through HTTPWebRequest. Error is that some pages need Script to be enabled. But how to do this in HTTPWebRequest class?
16
by: Cheung, Jeffrey Jing-Yen | last post by:
I have a windows form application that generates a request, downloads an image, and waits the user to enter in login info. Unfortunately, this image is dynamic and based on session data. I have...
1
by: Brett | last post by:
I'm checking a URL and logging the results returned from the server. One problem is when a site ask for cookies, my code times out. For example, this link does:...
5
by: bg | last post by:
Hi all. I 'm trying to automate some of my workflow, by doing some programmatic "POST"s to an internal webserver via HttpWebRequest. The Code is your usual straight forward type of thing. ...
3
by: JansenH | last post by:
We have implemented a 'HTTP Post' client in C# that posts Xml documents to a webserver. This is working fine if the post rate is one post for every 20 seconds. But if the post rate is increased to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.