473,756 Members | 5,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10008
Jack,

In my idea is this typical a question for the newsgroup

microsoft.publi c.dotnet.framew ork.aspnet

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

Cor

"Jack" <So********@new sgroups.nospams chree
f in bericht news:eS******** ******@TK2MSFTN GP06.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 maxRequestLengt h="10240" /<!-- change to 10MB -->

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

maxRequestLengt h:

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
"WebRequest 1 = 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.comwr ote in message
news:Vy******** ******@TK2MSFTN GXA01.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 maxRequestLengt h="10240" /<!-- change to 10MB -->

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

maxRequestLengt h:

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 .cookiecontaine r = 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 "Programmin g 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.comwr ote in message
news:cK******** ******@TK2MSFTN GXA01.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 - CookieCollectio n" 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
1590
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 = sr; myDataGrid.DataBind(); Do I need to explicitly close the DataReader (and thus the connection) after
3
19534
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 ExecuteCommand(string cmdStr) { Connect(); OracleCommand cmd = new OracleCommand(cmdStr, this._conn); OracleDataReader reader = cmd.ExecuteReader(); return reader; }
1
1492
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 (W2K3). It seems that now, for some reason, the connection cannot be established. The error logged is: The underlying connection was closed: Unable to connect to the remote server." source="System" stack="at...
4
3982
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, when should I it close it -- after the execution of the each command to the database? If leaving open, what impacts does it have. Imagining that it is one connection to the database. Thanks, mescano
5
2059
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 think I know the problem. My Connection remains open. Is there a way I can do to close the Connection. below is the code. Thank you very much as always
0
3908
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 crystal report, then I cannot check another checkbox to display report2 afterwards. If I close the website and open again, I can choose report2 and display as crystal report correct, but now I cannot check the checkbox1 to display report 1 afterwards....
3
2285
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 from vb6) and i receive the error that db is still in use becouse the connection is still actibe i still access to file via adodb in the routine in which i read the db the code is sub readdb
2
8060
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 receive". but what's wired i get this exception just after upload is finished when i try to close the stream that was used for upload. this happens only when upload takes more that certain amount of time. note that upload itself goes smooth no matter...
1
1693
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 connetion which I know is staying open even when i do this. WebProxy proxy = new WebProxy(m_proxyServer, 8080); //We do a GET to retrieve the ASP Session ID, and Viewstate and Event attributes states which is set in the...
0
9462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10046
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.