URL Encode with VB

Last post 11-04-2009 1:33 PM by mandrews1234. 4 replies.

Sort Posts:

  • URL Encode with VB

    10-30-2009, 11:20 AM

    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,

  • Re: URL Encode with VB

    10-30-2009, 1:23 PM
    • Contributor
      2,194 point Contributor
    • ratish_shriyan
    • Member since 10-05-2009, 8:55 AM
    • A D I T I Technologies, Bangalore
    • Posts 308

    Hi,

    Use the following method,

    String encodedURL = HttpServerUtility.HtmlEncode( url )

    More info here and here

    Hope this helps!


    Knowledge Is Power
  • Re: URL Encode with VB

    11-02-2009, 2:29 PM

    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?

  • Re: URL Encode with VB

    11-03-2009, 1:49 AM
    Answer

    mandrews1234:

    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

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread : )
  • Re: URL Encode with VB

    11-04-2009, 1:33 PM

    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,

Page 1 of 1 (5 items)