Scraping & passing cookies code
httpWebRequest.aspx
1 2 | <%@ Control Language="vb" AutoEventWireup="false" Codebehind="httpWebRequest.ascx.vb" Inherits="DamianM.httpWebRequest5" %> <asp:Literal id="Literal1" runat="server"></asp:Literal> |
httpWebRequest.aspx.vb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | Public Class httpWebRequest5 Inherits System.Web.UI.UserControl Public Class Webclient5 Inherits System.Web.UI.UserControl Protected WithEvents Literal1 As System.Web.UI.WebControls.Literal Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim wc As New System.Net.WebClient() Dim s As System.IO.Stream Dim sr As System.IO.StreamReader 'set cookie value in header wc.Headers.Add("Cookie", "value=this is a test;value2=this is another test") 'scrape the data s = wc.OpenRead("http://www.damianm.co.uk/code/4.5/cookiereader.aspx") 'read in the page sr = New System.IO.StreamReader(s) Literal1.Text = sr.ReadToEnd() End Sub End Class Protected WithEvents Literal1 As System.Web.UI.WebControls.Literal Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim req As System.Net.HttpWebRequest Dim res As System.Net.HttpWebResponse Dim sr As System.IO.StreamReader Dim s As System.IO.Stream Dim c As New System.Net.Cookie() '--notice that the instance is created using webrequest '--this is what microsoft recomends req = System.Net.WebRequest.Create("http://www.damianm.co.uk/code/4.5/cookiereader.aspx") 'create a cookie container to hold the cookie 'this will override the header is you try to set cookie directly 'if you do not pass up a cookie container, 'the response will not fill out the cookie collection Dim cc As New System.Net.CookieContainer() c.Name = "value" c.Value = "this is a test" c.Domain = "www.aspalliance.com" c.Secure = True cc.Add(c) c.Name = "value2" c.Value = "this is another test" cc.Add(c) req.CookieContainer = cc res = req.GetResponse() 'read in the page sr = New System.IO.StreamReader(res.GetResponseStream()) Literal1.Text = sr.ReadToEnd '--tidy up sr.Close() res.Close() End Sub End Class |
Webclient.aspx
1 2 | <%@ Control Language="vb" AutoEventWireup="false" Codebehind="Webclient.ascx.vb" Inherits="DamianM.Webclient5" %> <asp:Literal id="Literal1" runat="server"></asp:Literal> |
Webclient.aspx.vb
1 |



















