473,396 Members | 1,810 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,396 software developers and data experts.

Net.WebRequest - Close Connection

I have a WebRequest object that I use to log into a site and then post some
XML. In doing this I set the KeepAlive = true so that it maintains the
connection and does operates undo the initial login.

Is there a way to force this connection to close after I an done with it.
What happens is that I may need to post the XML in several chunks due to
size therefore I cannot set the KeepAlive to False when I post the XML and
the way the code runs I do not know which pass is the last.

What happens is that the first post works fine but subsequent posts time
out, I assume this is due to the connection timing out.

Any ideas, or a better way to do this?

Jack
Aug 28 '06 #1
6 9950
Jack,

In my idea is this typical a question for the newsgroup

microsoft.public.dotnet.framework.aspnet

Just an advice of course feel free what you do with it.

Cor

"Jack" <So********@newsgroups.nospamschree
f in bericht news:eS**************@TK2MSFTNGP06.phx.gbl...
>I have a WebRequest object that I use to log into a site and then post some
XML. In doing this I set the KeepAlive = true so that it maintains the
connection and does operates undo the initial login.

Is there a way to force this connection to close after I an done with it.
What happens is that I may need to post the XML in several chunks due to
size therefore I cannot set the KeepAlive to False when I post the XML and
the way the code runs I do not know which pass is the last.

What happens is that the first post works fine but subsequent posts time
out, I assume this is due to the connection timing out.

Any ideas, or a better way to do this?

Jack

Aug 29 '06 #2
Hi Jack,

From your post title and first part, your question seems to "how to force
the connection close".

By default, HttpWebRequest.KeepAlive is true. So the connections should be
reused from one request to the next. And I don't think you need to
explicitly close the connection, just dispose the HttpWebRequest object.

Also, note that the server could also be closing the connection after
serving each request. Keep-alive can be set in the http request header, but
server can decide whether or not to reuse the connection.

Reusing connection is to improve performance. I don't think it's required
to maintain the login credential. As long as you are using the same
HttpWebRequest object, the credential should be used in subsequent
requests.

As your last question about "subsequent posts time out", I think this is
probably because the request size limit. If your server is written in
ASP.NET, the default maximum request size is 4MB. You can change this limit
in web.config:

<system.web>
<httpRuntime maxRequestLength="10240" /<!-- change to 10MB -->

http://msdn2.microsoft.com/en-us/library/e1f13641.aspx

maxRequestLength:

Specifies the limit for the input stream buffering threshold, in KB.
This limit can be used to prevent denial of service attacks that are
caused, for example, by users posting large files to the server.

Also, you can use WebClient to do the post:
http://msdn2.microsoft.com/en-us/library/tt0f69eh.aspx

If my assumption is not the case, I think a repro project would be very
helpful for us to diagnose the issue quickly.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 29 '06 #3
I may be missing something with regard to how the webrequest is used. What
I did is make the webrequest a global since I go in and out of the sub and
want to maintain the session across these calls. The first request (a
Post), second (a Get) and third (a Post) work fine, but if I try to reenter
and execute the same requests again they time out. I am executing a
"WebRequest1 = Net.WebRequest.Create(strUrl)" on each of the requests since
they go to different URLs, is that correct? Is there something that I am
just not getting about how this works and how the session is kept active?

Jack
"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:Vy**************@TK2MSFTNGXA01.phx.gbl...
Hi Jack,

From your post title and first part, your question seems to "how to force
the connection close".

By default, HttpWebRequest.KeepAlive is true. So the connections should be
reused from one request to the next. And I don't think you need to
explicitly close the connection, just dispose the HttpWebRequest object.

Also, note that the server could also be closing the connection after
serving each request. Keep-alive can be set in the http request header,
but
server can decide whether or not to reuse the connection.

Reusing connection is to improve performance. I don't think it's required
to maintain the login credential. As long as you are using the same
HttpWebRequest object, the credential should be used in subsequent
requests.

As your last question about "subsequent posts time out", I think this is
probably because the request size limit. If your server is written in
ASP.NET, the default maximum request size is 4MB. You can change this
limit
in web.config:

<system.web>
<httpRuntime maxRequestLength="10240" /<!-- change to 10MB -->

http://msdn2.microsoft.com/en-us/library/e1f13641.aspx

maxRequestLength:

Specifies the limit for the input stream buffering threshold, in KB.
This limit can be used to prevent denial of service attacks that are
caused, for example, by users posting large files to the server.

Also, you can use WebClient to do the post:
http://msdn2.microsoft.com/en-us/library/tt0f69eh.aspx

If my assumption is not the case, I think a repro project would be very
helpful for us to diagnose the issue quickly.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Sep 1 '06 #4
Hi Jack,

Although you make the webrequest a global variable, but you are recreating
it on each request by Net.WebRequest.Create(strUrl).

To maintain the session after you first logged on, you need to use a
CookieContainer to store the cookies you obtained after the first request
to logon. For more information, see
http://blogs.vbcity.com/drydo/archiv...9/27/5556.aspx

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 4 '06 #5
I looked at the code in the link you attached and that is basically what I
am doing for my logon and response. How would I compose my next post
request? Do I use .create(url) and then set .cookiecontainer = CookieC?

Also, is there a good resource on coding this? I have purchased "MS Visual
Basic 2005: The Language", "101 Visual Basic .NET Applications", "Visual
Studio Tools for Office" and A! Apress's "Programming the Web with Visual
Basic .NET"; none of which really seem to take about using the WebRequest
object to access secure web resources inside a VB.NET Windows Application.

Jack

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:cK**************@TK2MSFTNGXA01.phx.gbl...
Hi Jack,

Although you make the webrequest a global variable, but you are recreating
it on each request by Net.WebRequest.Create(strUrl).

To maintain the session after you first logged on, you need to use a
CookieContainer to store the cookies you obtained after the first request
to logon. For more information, see
http://blogs.vbcity.com/drydo/archiv...9/27/5556.aspx

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Sep 5 '06 #6
Hi Jack,

Thank you for your update. You are right that you need to pass the
CookieContainer object to your second request.

When using System.Net or Web Services, you might want to receive or send
cookies, perhaps for session state maintenance or in some rate situations
for proxy authentication. CookieContainer is designed to be a store of all
the cookies for one or more requests. To that extent, the CookieContainer
is designed to be a Hashtable of "domain - CookieCollection" pairs.

For more information, please refer to following article:

#Cookies, Cookie Collection and CookieContainer
http://blogs.msdn.com/dgorti/archive...16/452347.aspx

And here're some other KB articles related to CookieContainer you may find
useful:

#How to authenticate the Inbox in Exchange Server 2003 with forms-based
authentication enabled by using Visual Basic.NET
http://support.microsoft.com/kb/891748/

#HOW TO: Use CookieContainer to Maintain a State in Web Services Using
Visual Basic.NET
http://support.microsoft.com/kb/820528/

Hope this helps. Please feel free to post here if anything is unclear.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: billd | last post by:
I have a function that returns a SqlDataReader created by: result = command.ExecuteReader(CommandBehavior.CloseConnection); I then bind the result to a grid control: myDataGrid.DataSource =...
3
by: Craig | last post by:
I have some methods that open a database connection, get some data and then return a datareader. How do I manage closing the connection to the database then? public OracleDataReader...
1
by: GSK | last post by:
This one has me stumped: I am using HttpWebRequest to resolve an external URL (that outputs an XML string). It works fine on my dev machine (W2K), and used to work on my production machine...
4
by: mescano | last post by:
I am currently implementing a singleton pattern for accessing a database. Is it advisable to close the connection to the database at all -- thus leaving it open or should it be closed. If closed,...
5
by: Varangian | last post by:
Hello there people, I'm having some kind of problem. I have a function that returns a datareader. At some point using the application I get an error "Unspecified error" (ssssoooo helpful) :). I...
0
by: bonita | last post by:
In my ASP.NET page, I have 2 checkboxes for users to choose which crystal report they want to display. These two reports use different tables. If report1 has been choosen and displayed in the...
3
by: DavideR | last post by:
I'm working with vs2005 (vb.net) i need to detach a database autoclose property is set to true close cursor on commit is set to true i use the sp_detach with adodb (the program has been converted...
2
by: SharpCoderMP | last post by:
I'm having troubles with uploading files to the ftp server using FtpWebRequest. I occasionally get an Exception saying: "The underlying connection was closed: An unexpected error occurred on a...
1
by: twomster | last post by:
Hi, I am using the above object to different POST and GET request. Basically I am downloading a file. I want to download about 10 different files. However I want to be able to close the...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.