• The eternal confessions of a beautiful mind...
  • DamianM.Co.UK
  • Home
  • About
  • Archives
  • Contact
  • Sitemap
  • My Flickr

    IMG_9585IMG_9115IMG_9113IMG_9111IMG_9078IMG_9075IMG_9069IMG_9065IMG_9041IMG_9032IMG_8963IMG_8928IMG_8916IMG_8915IMG_8904IMG_8876IMG_8858IMG_8830IMG_8828IMG_8826

  • Recent Posts

    • New Ohio Roller Coaster - INSANE!!!
    • Speeding
    • Captions not required
    • Unfortunate Backgrounds
    • Unfortunate Signs
    • OMG!!
    • Women as explained by engineer
    • The Monty Hall Problem
    • ahhh still love them - Motivational Posters
    • What does Mona Lisa do when the Museum is closed………
    • 21st Century kids books
    • Cool Origami
    • AWESOME Pictures!
    • Things you shouldn’t find in your vegetable patch!
    • World’s Best Graffiti…?
  • My Tools

    • Blog_LinkIt
    • DCoda Theme
    • DCoda Widgets
    • RSS_Sticky
    • WordPress.org
    • WP_BlogNetworking
    • WP_BlogRollSync
    • WP_BoilerPlate
    • WP_Censor
    • WP_ContactMe
    • WP_DeliciousPost
    • WP_EasyReply
    • WP_HeadNFoot
    • WP_LinkIt
    • WP_OneInstall
    • WP_PostDate
    • WP_PostNotes
    • WP_RssSticky
    • WP_Spoiler
    • WP_Submission
  • My Web

    • ASPAlliance
    • ClaimID
    • del.ico.us
    • Digg
    • DSLRBlog
    • DVDProfiler
    • Flickr
    • Honeyed SPAM
    • My Blog
    • My company
    • MYSpace
    • WordPress.org
    • YouTube

    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
    
     

    Leave a Reply

    Related Posts from the Past:

    • .NET Screen Scraping in depth
    • Scraping in one line using temporary objects code
    • MiniMo the Mobile Firefox
    • CSS: Standards
    • Passing Headers Code