locked
URL Encode with VB RRS feed

  • Question

  • User565442435 posted

    I need to add post data to a link and then post to it via visual basic. Here is a little example of what I am trying to do.

    Let’s say I have a job with the following info

    name : job name 1

    department: administrative

    description: some description here, could be very long

    hours: part time

    pay: 10$/hr


    This is the link to add a new job

    https://example.com/action=new

     

    I need to add URL-encoded information like this.

    po_name=job%20name%201&desc=some%20description%20here,%20could%20be%20very%20long&dept=administrative&part_time=yes&salary=10$/hr

    All of that is post data. I really have no idea where to start on this and need to be pointed in the right direction. Can anyone help me out? Thanks,

    Friday, October 30, 2009 11:20 AM

Answers

  • User1006193418 posted

    Thanks for the encode method. What about adding POST data? I need to assign post variables and their values, do you know how to do that?

    Hi,

    To send values via POST mode to a website, we need to use WebRequest Class. Please have a try on the demo below. It should help you with the task.

    Dim postData As String = "job%20name%201&desc=some%20description%20here,%20could%20be%20very%20long&dept=administrative&part_time=yes&salary=10$/hr"
    postData = Server.UrlDecode(postData)
    
    Dim encoding = New ASCIIEncoding
    Dim data As Byte() = encoding.GetBytes(postData)
    
    Dim myRequest As Net.WebRequest = Net.WebRequest.Create("")
    
    With myRequest
        .Method = "POST"
        .ContentType = "application/x-www-form-urlencoded"
        .ContentLength = data.Length
    End With
    
    Dim newStream As IO.Stream = myRequest.GetRequestStream()
    newStream.Write(data, 0, data.Length)
    newStream.Close()
    
    Dim myResponse As Net.WebResponse = CType(myRequest.GetResponse, Net.WebResponse)
    
    Dim reader As IO.StreamReader = New IO.StreamReader(myResponse.GetResponseStream(), Text.Encoding.Default)
    Dim content As String = reader.ReadToEnd()

    Please refer to this link for more information about WebResponse class: http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 3, 2009 1:49 AM

All replies

  • User2077888689 posted

    Hi,

    Use the following method,

    String encodedURL = HttpServerUtility.HtmlEncode( url )

    More info here and here

    Hope this helps!


    Friday, October 30, 2009 1:23 PM
  • User565442435 posted

    Thanks for the encode method. What about adding POST data? I need to assign post variables and their values, do you know how to do that?

    Monday, November 2, 2009 2:29 PM
  • User1006193418 posted

    Thanks for the encode method. What about adding POST data? I need to assign post variables and their values, do you know how to do that?

    Hi,

    To send values via POST mode to a website, we need to use WebRequest Class. Please have a try on the demo below. It should help you with the task.

    Dim postData As String = "job%20name%201&desc=some%20description%20here,%20could%20be%20very%20long&dept=administrative&part_time=yes&salary=10$/hr"
    postData = Server.UrlDecode(postData)
    
    Dim encoding = New ASCIIEncoding
    Dim data As Byte() = encoding.GetBytes(postData)
    
    Dim myRequest As Net.WebRequest = Net.WebRequest.Create("")
    
    With myRequest
        .Method = "POST"
        .ContentType = "application/x-www-form-urlencoded"
        .ContentLength = data.Length
    End With
    
    Dim newStream As IO.Stream = myRequest.GetRequestStream()
    newStream.Write(data, 0, data.Length)
    newStream.Close()
    
    Dim myResponse As Net.WebResponse = CType(myRequest.GetResponse, Net.WebResponse)
    
    Dim reader As IO.StreamReader = New IO.StreamReader(myResponse.GetResponseStream(), Text.Encoding.Default)
    Dim content As String = reader.ReadToEnd()

    Please refer to this link for more information about WebResponse class: http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 3, 2009 1:49 AM
  • User565442435 posted

    Thanks for all of your responses. In case someone else comes across this, here is a simple quick and dirty example.

    Dim myWebClient As New System.Net.WebClient()
    
    myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
    
    Dim postQuery As Byte() = System.Text.Encoding.ASCII.GetBytes("action=login&username=marcel&password=password")
    Dim theResponse As Byte() = myWebClient.UploadData("http://www.webmasterworld.com/login.cgi", "POST", postQuery )
    
    Response.Write(System.Text.Encoding.ASCII.GetString(theResponse)) 

    That will acutally log you into that site, provided you give proper credentials
    Note: I need to credit http://www.webmasterworld.com/microsoft_asp_net/4017629.htm

    If anyone ever can't find an answer on here (which doesn't happen very oftent) I would recommend that site also. I usually post to both to get a fast response. Thanks,

    Wednesday, November 4, 2009 1:33 PM