Vb.NET Httpwebrequest Post

Posted on 2011-05-06
Last Modified: 2012-06-27
hi all.
I use the webbrowser in my applications
WebBrowser1.Document.All("username11").InnerText = "user"
 WebBrowser1.Document.All("password11").InnerText = "pass"

Open in new window

How I can use httpwebrequest to do it????
Question by:Replica178

    Author Comment

    LVL 17

    Expert Comment

    by:Carlos Villegas
    Hi, I did that to interact my application with a customer web application, I will give you an example.
    LVL 17

    Accepted Solution

    Hi, I made this custom WebClient class, this will keep the cookies state to do more easy to work with the HttpRequest and HttpResponse (it will keep track of your session if there is one), also it set the the User-Agent header as Internet Explorer 8:
    Imports System.Net
    Public Class MyWebClient
        Inherits WebClient
        Dim mCookieContainer As New CookieContainer()
        Public Property CookieContainer() As CookieContainer
                Return mCookieContainer
            End Get
            Set(ByVal value As CookieContainer)
            End Set
        End Property
        Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
            Dim request As HttpWebRequest = MyBase.GetWebRequest(address)
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)"
            request.CookieContainer = Me.CookieContainer
            Return request
        End Function
    End Class

    Open in new window

    An example of how to use MyWebClient class:
    Using myWebClient As New MyWebClient
        Dim myUrl As String = ""
        ' Your form values.
        Dim values As New NameValueCollection
        values.Add("username11", "MyUserName")
        values.Add("Password11", "MyPassword")
        ' You can control the cookies by using:
        ' Also can be required that you made a first request to get a session cookie before post your data.
        ' Uncomment the line below to get your session id (the site must support it).
        ' POST the data and get the response
        Dim responseData() As Byte = myWebClient.UploadValues(myUrl, values)
        ' Convert to response to text.
        Dim responseText As String = System.Text.Encoding.UTF8.GetString(responseData)
        'Show in the console
    End Using

    Open in new window

    I hope this help!

    Author Comment

    thanks.i will try it

    Author Closing Comment

    LVL 17

    Expert Comment

    by:Carlos Villegas
    You are welcome buddy

    Author Comment

    Sorry.I dont Understand what is it NameValueCollection ?? i get error
    LVL 17

    Expert Comment

    by:Carlos Villegas
    Please include this in your code:
    Imports System.Collections.Specialized

    Open in new window


    Author Comment

    thanks.Now its work perfectly.thank you very much

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now