Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / April 2005

Tip: Looking for answers? Try searching our database.

How to make multiple or consecutive webrequests using the same connection or session

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
emcinerney@yahoo.com - 26 Apr 2005 01:06 GMT
I'm attempting to make WebRequest or HttpWebRequest requests to a
router's HTTP configuration interface. I cannot navigate directly to
the page I need because the router will automatically redirect
(somehow...I'm not sure if it's an http redirect or what) the request
to the router's initial page. So, after the first request, I need to
make a second request to navigate to the page I want. However, the
connection/session to the router seems to be lost and it again
redirects me to the router's initial page.

I have tried setting AllowAutoRedirect to False, but that didn't help.

Code Sample:

-----
Dim WR As HttpWebRequest
Dim SR As System.io.StreamReader

'This makes the first request
WR = CType(WebRequest.Create(sDestinationURI), HttpWebRequest)
WR.Credentials = New Net.NetworkCredential(Me.txtRouterUserID.Text,
Me.txtRouterPW.Text)

SR = New System.IO.StreamReader(WR.GetResponse.GetResponseStream)
Me.txtRouterHTMLSource.Text = WR.GetResponse.Headers.ToString()
Me.txtRouterHTMLSource.Text += SR.ReadToEnd
SR.Close()

'This makes the 2nd request, but I still get returned to the
'initial page.
WR = CType(WebRequest.Create(sDestinationURI), HttpWebRequest)
WR.Credentials = New Net.NetworkCredential(Me.txtRouterUserID.Text,
Me.txtRouterPW.Text)

SR = New System.IO.StreamReader(WR.GetResponse.GetResponseStream)
Me.txtRouterHTMLSource.Text = WR.GetResponse.Headers.ToString()
Me.txtRouterHTMLSource.Text += SR.ReadToEnd
SR.Close()

-----

Thanks for any and all help.

-Ed
recoil@community.nospam - 26 Apr 2005 19:04 GMT
What you need to do is Persist the Credentails and the cookies. I
believe this is done by grabbing the cookies from the prior request and
then setting them on the new request.
emcinerney@yahoo.com - 27 Apr 2005 04:06 GMT
Thanks for the suggestion, but I still couldn't get it to work. I
created variables for CookieContainer and Credentials and set them from
the request's response. Then I used those variables to set those
properties on the new request object. The 2nd request works, but I
still get the initial page back. Oh, and when I checked the
CookieContainer object, the count was 0, so I don't think it's using
cookies. Any other suggestions?

> What you need to do is Persist the Credentails and the cookies. I
> believe this is done by grabbing the cookies from the prior request and
> then setting them on the new request.
Nathan S - 27 Apr 2005 15:08 GMT
Why do you feel the second request should get a different resonse to th
first one?  (from your code, it looks like you're requesting the sam
uri twice)

If the second request's uri is different, then maybe you're gettin
redirected because you haven't authenticated properly?

N

--
Nathan
Feroze [msft] - 29 Apr 2005 23:57 GMT
What I would suggest is: try the same scenario with a browser, and sniff the
session with ethereal. Then, use the trace to figure out how the server is
persisting state to the client. Is it using

1) cookies
2) credentials
3) Connection persistence
4) some/all of the above?

If it is using cookies, make sure you set a CookieContainer on the
webrequest. For credentials, you will need to set a NetworkCredential.

For connection persistence, you can use the ConnectionGroupName property.
Set it to a unique name, and use the same name for all requests that you
want to share the connection.

Signature

feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------

> I'm attempting to make WebRequest or HttpWebRequest requests to a
> router's HTTP configuration interface. I cannot navigate directly to
[quoted text clipped - 39 lines]
>
> -Ed

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.