Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

Vb.NET Httpwebrequest Post

hi all.
I use the webbrowser in my applications
Ex.
WebBrowser1.Document.All("username11").InnerText = "user"
 WebBrowser1.Document.All("password11").InnerText = "pass"

Open in new window

How I can use httpwebrequest to do it????
0
Replica178
Asked:
Replica178
  • 5
  • 4
1 Solution
 
Replica178Author Commented:
UP
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi, I did that to interact my application with a customer web application, I will give you an example.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
        Get
            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 = "http://www.asp.net"

    ' Your form values.
    Dim values As New NameValueCollection
    values.Add("username11", "MyUserName")
    values.Add("Password11", "MyPassword")

    ' You can control the cookies by using:
    'myWebClient.CookieContainer

    ' 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).
    'myWebClient.DownloadData(myUrl)

    ' 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
    Console.WriteLine(responseText)
End Using

Open in new window

I hope this help!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Replica178Author Commented:
thanks.i will try it
0
 
Replica178Author Commented:
Thanks
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
You are welcome buddy
0
 
Replica178Author Commented:
Sorry.I dont Understand what is it NameValueCollection ?? i get error
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Please include this in your code:
Imports System.Collections.Specialized

Open in new window

0
 
Replica178Author Commented:
thanks.Now its work perfectly.thank you very much
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now