Only receive cookies from webrequests


I

Issos

Is there a way to just receive the cookie(s) from an webrequest in
ASP.NET? The function I'm using at the moment can handle cookies... but
it will also get the full response of the request... I would like to be
able to use an extra boolean parameter like "onlyCookies" to select the
"type" of webrequest...

Private oCookies As CookieCollection



Public Function getPage(ByVal url As String) As String

Dim loHttp As HttpWebRequest =
CType(WebRequest.Create(url.Trim), HttpWebRequest)
loHttp.Timeout = 10000
loHttp.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; SV1; .NET CLR 2.0.50727; InfoPath.1; .NET CLR
1.1.4322)"
loHttp.CookieContainer = New CookieContainer

If Not (Me.oCookies Is Nothing) AndAlso Me.oCookies.Count >
0 Then
loHttp.CookieContainer.Add(Me.oCookies)
End If

Dim loWebResponse As HttpWebResponse =
CType(loHttp.GetResponse, HttpWebResponse)
If loWebResponse.Cookies.Count > 0 Then
If Me.oCookies Is Nothing Then
Me.oCookies = loWebResponse.Cookies
Else
For Each oRespCookie As Cookie In
loWebResponse.Cookies
Dim bMatch As Boolean = False
For Each oReqCookie As Cookie In Me.oCookies
If oReqCookie.Name = oRespCookie.Name Then
oReqCookie.Value = oRespCookie.Name
bMatch = True
End If
Next
If Not bMatch Then
Me.oCookies.Add(oRespCookie)
End If
Next
End If
End If

Dim enc As Encoding = Encoding.GetEncoding(1252)
If loWebResponse.ContentEncoding.Length > 0 Then
enc =
Encoding.GetEncoding(loWebResponse.ContentEncoding)
End If
Dim loResponseStream As StreamReader = New
StreamReader(loWebResponse.GetResponseStream, enc)
getPage = loResponseStream.ReadToEnd
loResponseStream.Close()
loWebResponse.Close()
Return getPage
End Function
 
Ad

Advertisements

Ad

Advertisements

J

Joerg Jooss

Thus wrote Issos,
Is there a way to just receive the cookie(s) from an webrequest in
ASP.NET? The function I'm using at the moment can handle cookies...
but it will also get the full response of the request... I would like
to be able to use an extra boolean parameter like "onlyCookies" to
select the "type" of webrequest...
[...]

Send a HEAD request -- that will return inly the HTTP headers, but no body.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top