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

  • Thread starter Thread starter emcinerney
  • Start date Start date
E

emcinerney

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()
 
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.
 
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 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.
 
Back
Top