Results 1 to 13 of 13

Thread: [RESOLVED] Submit form using HttpWebRequest

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Resolved [RESOLVED] Submit form using HttpWebRequest

    The below code allows me to login to a site using HttpWebRequest however I can't login to the site https://www.comsec.com.au (which is the one I want to login to). At the bottom of the code I write the source to a file and upon opening it simply shows the login page.

    After looking at the source I believe the issue is caused by the form calling a javascript funciton to actually submit the form when the 'Login' button is clicked. Has anyone had this type of situation in the past and if so what was the solution to get the form to submit?


    vb Code:
    1. Dim url As String = "https://www.comsec.com.au/default.aspx"
    2.         Dim data As String = "UcHeader1$LoginName=MyLogin&UcHeader1$Password=MyPassword&UcHeader1$StartIn="""
    3.    
    4.         Dim buffer As Byte() = Encoding.UTF8.GetBytes(data)
    5.         Dim result As String = ""
    6.         Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
    7.         req.Method = "POST"
    8.         req.ContentType = "application/x-www-form-urlencoded"
    9.         req.ContentLength = buffer.Length
    10.         'req.Proxy = new WebProxy(proxy, true); // ignore for local addresses
    11.         req.CookieContainer = New CookieContainer()
    12.         ' enable cookies
    13.         Dim reqst As Stream = req.GetRequestStream()
    14.         ' add form data to request stream
    15.         reqst.Write(buffer, 0, buffer.Length)
    16.         reqst.Flush()
    17.         reqst.Close()
    18.  
    19.         Console.WriteLine(vbLf & "Posting data to " & url)
    20.         Dim res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
    21.         ' send request,get response
    22.         Console.WriteLine(vbLf & "Response stream is: " & vbLf)
    23.         Dim resst As Stream = res.GetResponseStream()
    24.         ' display HTTP response
    25.         Dim sr As New StreamReader(resst)
    26.         result = sr.ReadToEnd()
    27.         Using writer As System.IO.StreamWriter = New StreamWriter("C:\startOut.html")
    28.             writer.Write(result)
    29.         End Using

  2. #2

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Submit form using HttpWebRequest

    I've searched the web all day and still can't find a soltion.....anyone able to help me out?

  3. #3
    New Member
    Join Date
    Nov 2009
    Location
    In the middle of a corn field
    Posts
    2

    Re: Submit form using HttpWebRequest

    Did you ever get this solved? I have a similar situation and was curious to know how you solved it. Please let me know.

    Thanks
    -mb

  4. #4
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Submit form using HttpWebRequest

    maybe it's irrelevant but i'm curious to know this:
    as U said that this approach works to some other sites but not on this one can you test if you can open it like this from your webbrowser:
    Code:
    'data to send:
    'Dim url As String = "https://www.comsec.com.au/default.aspx"
    'Dim data As String = "UcHeader1$LoginName=MyLogin&UcHeader1$Password=MyPassword&UcHeader1$StartIn="""
    
    data to type in your browser to test if its working:
    https://www.comsec.com.au/default.aspxUcHeader1$LoginName=MyLogin&UcHeader1$Password=MyPassword&UcHeader1$StartIn="
    When I use stuff like this with user pass in url encoded I try to test it first in my browser to see if its working...
    But in your case I see that this site uses secured http connection so IMO this is not possible because you don't have in your console app needed certificates that says that you are an trusted connection...
    That site must first evaluate that certificate to continue, then is your name/pass evaluated and only then you can get in...
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  5. #5
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Submit form using HttpWebRequest

    Some reading:
    HTTPS protocol
    Certificate on that site is Commonwealth Securities Limited Certificate which is verified by VeriSign.
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  6. #6

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Resolved Re: Submit form using HttpWebRequest

    mb2000inc, yes I have managed to solve my issue with a work around. This is how I did it...

    1. Added a hidden websbrowser control to my form and loaded the page.
    2. Simulated enteing my login details and clicking the login button via code.
    3. Once logged in I grabbed all the cookies that got set (about 15 of them!) and added them to a CookieContainer.
    4. Added the CookieContainer to a HttpWebRequest and then now I can get the source of any page as if I had loaded the page in my browser and selected View Page Source

  7. #7
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: [RESOLVED] Submit form using HttpWebRequest

    nice workaround
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  8. #8
    New Member
    Join Date
    Nov 2009
    Location
    In the middle of a corn field
    Posts
    2

    Re: [RESOLVED] Submit form using HttpWebRequest

    Excellent and impressive. I will give that a shot with my issue.

  9. #9
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Question Re: Submit form using HttpWebRequest

    Quote Originally Posted by lintz View Post
    mb2000inc, yes I have managed to solve my issue with a work around. This is how I did it...

    1. Added a hidden websbrowser control to my form and loaded the page.
    2. Simulated enteing my login details and clicking the login button via code.
    3. Once logged in I grabbed all the cookies that got set (about 15 of them!) and added them to a CookieContainer.
    4. Added the CookieContainer to a HttpWebRequest and then now I can get the source of any page as if I had loaded the page in my browser and selected View Page Source
    well i done something similar to this but i'm stuck at point 3., in my case i have 13 cookies in my cookie folder, how do i add these cookies to the cookiecontainer? is it a case of reading these with a filestream? some example code would be appreciated for this step

  10. #10

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: [RESOLVED] Submit form using HttpWebRequest

    This the code I'm using for step 3.....HTH


    vb Code:
    1. 'Define cookie container
    2. Public ocookies As CookieContainer = New CookieContainer
    3. Public YumYum As String 'the cookies
    4.  
    5. 'Get the cookies once you have logged in from the WB control
    6. Public Sub WB1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WB1.DocumentCompleted
    7.  
    8.         If Me.WB1.ReadyState = WebBrowserReadyState.Complete Then
    9.             WebPageLoaded = True
    10.  
    11.             'read the cookies that have been saved by the webpage
    12.             YumYum = WB1.Document.Cookie
    13.            
    14.         End If
    15.  
    16.  
    17.     End Sub
    18.  
    19. 'Then add cooklies to cookie container
    20. Private Sub AddCookiesToCookieJar()
    21.  
    22.         Try
    23.             Dim IECookies As String()
    24.             IECookies = YumYum.Split(New String() {"; "}, StringSplitOptions.RemoveEmptyEntries)
    25.             For i As Integer = 0 To IECookies.Length - 1
    26.                 Dim TheCookie As String() = IECookies(i).Split(New Char() {"="c}, 2, StringSplitOptions.RemoveEmptyEntries)
    27.  
    28.                ocookies.Add(AddComsecCookies(TheCookie(0), TheCookie(1)))
    29.                
    30.             Next
    31.  
    32.  
    33.         Catch ex As Exception
    34.             MessageBox.Show(ex.Message)
    35.         End Try
    36.  
    37.     End Sub
    38.  
    39.  
    40. Private Function AddComsecCookies(ByVal name As String, ByVal value As String) As Cookie
    41.         Dim oc As New Cookie
    42.         Dim ExpireDate As String = "#" & DateAdd(DateInterval.Day, 1, Today.Date) & " 1:30:00 PM#"
    43.         oc.Domain = [url]www.comsec.com.au[/url]
    44.         oc.Expires = ExpireDate '("#11/3/2009 1:17:10 PM#")
    45.         oc.Name = name
    46.         oc.Path = "/"
    47.         oc.Secure = False
    48.         oc.Value = value
    49.  
    50.         Return oc
    51.     End Function

  11. #11
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: [RESOLVED] Submit form using HttpWebRequest

    ty lintz great solution to this issue , I found out btw how to do this using just httpwebrequest but it only gets 10 of my 13 cookies :s this method will get them all

  12. #12
    Member
    Join Date
    May 2010
    Posts
    56

    Re: [RESOLVED] Submit form using HttpWebRequest

    Hi, I am sorry to reopen this post but I have a very similar issue and I am trying to figure out your 4th step.

    I grabbed all my cookies and I see from your code that you split them one by one. What it's not clear to me is how do you use the cookie container and how you send the GET request (as it's not shown in the code). I am sorry but I am quite a newbie in vb

    Thanks for your help
    Last edited by kiwi1342; May 11th, 2010 at 03:12 AM.

  13. #13
    Member
    Join Date
    May 2010
    Posts
    56

    Re: [RESOLVED] Submit form using HttpWebRequest

    ok I solved it. the solution is here:

    http://www.vbforums.com/showthread.php?t=614217

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width