Results 1 to 6 of 6

Thread: encoding a URL

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    encoding a URL

    I found out that firefox will not open if you try to launch it with a command line param that is a url with a querystring that is not encoded

    for example

    if i launch IE using the following URL
    http://www.vbforums.com?id=hello world
    this works fine

    now firefox will crap out because of the space in hello world (on my system at least firefox doesnt even open, although it returns a process handle)

    so if i encode the URL i should have no issues on either browser..

    i tried using the System.Web.HttpUtility.UrlEncode method but that encodes the entire URL so it looks like this
    http%3a%2f%2fwww.vbforums.com%3fid%3dhello+world

    so is my only option to parse the string into 2 parts (site, and query string), then call call the urlencode on the querystring and put them back together? or is there another method I can use?

  2. #2
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537

    Re: encoding a URL

    I have not found a urlencode method yet in VB.NET although there should be one.

    I have though done this with great success for spaces:
    Code:
    Str = Str.Replace(" ","%20")
    ...
    ...
    You could then just go down the line of invalid characters like "&".

    W3C should have something on the list of chars and their values.

    Hope this helps.
    In life you can be sure of only two things... death and taxes. I'll take death.

  3. #3
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: encoding a URL

    VB Code:
    1. Dim url As New UriBuilder("http://www.vbforums.com?id=hello world")
    2.  
    3.         MsgBox(url.Uri.ToString)

    If you pass the url to the constructor of a UriBuilder it will automagically escape the Query parameters.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: encoding a URL

    It works fine for me if i enclose it in quote:
    Code:
    >firefox -url "http://www.vbforums.com/?id=hello world"
    or
    Code:
    >firefox -url "http://www.vbforums.com/?id=hello+world"
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: encoding a URL

    I also wrote Woka a URL encoding function. The only parts of the URL you encode though at the name value pairs. I.e:
    Code:
    http://www.vbforums.com/?encode%20this=hello&html=%3Chtml%3E
    Sorry, they are in VB6 though not .NET:
    VB Code:
    1. Function URLEncode(ByVal str As String) As String
    2.         Dim intLen As Integer
    3.         Dim x As Integer
    4.         Dim curChar As Long
    5.        
    6.         Dim newStr As String
    7.        
    8.         intLen = Len(str)
    9.         newStr = ""
    10.                
    11.         For x = 1 To intLen
    12.             curChar = Asc(Mid$(str, x, 1))
    13.            
    14.             If (curChar < 48 Or curChar > 57) And _
    15.                 (curChar < 65 Or curChar > 90) And _
    16.                 (curChar < 97 Or curChar > 122) Then
    17.                
    18.                 newStr = newStr & "%" & Hex(curChar)
    19.             Else
    20.                 newStr = newStr & Chr(curChar)
    21.             End If
    22.         Next x
    23.        
    24.         URLEncode = newStr
    25. End Function
    26.  
    27. Function UrlDecode(ByVal str As String) As String
    28.         Dim intLen As Integer
    29.         Dim x As Integer
    30.         Dim curChar As String * 1
    31.         Dim strCode As String * 2
    32.        
    33.         Dim newStr As String
    34.                 intLen = Len(str)
    35.         newStr = ""
    36.                 For x = 1 To intLen
    37.             curChar = Mid$(str, x, 1)
    38.             If curChar = "%" Then
    39.                 strCode = "&h" & Mid$(str, x + 1, 2)
    40.                
    41.                 If IsNumeric(strCode) Then
    42.                     curChar = Chr(Int(strCode))
    43.                 Else
    44.                     curChar = ""
    45.                 End If
    46.                                 x = x + 2
    47.             End If
    48.                         newStr = newStr & curChar
    49.         Next x
    50.        
    51.         UrlDecode = newStr
    52. End Function
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: encoding a URL

    Quote Originally Posted by <ABX
    VB Code:
    1. Dim url As New UriBuilder(&quot;[url]http://www.vbforums.com?id=hello[/url] world&quot;)
    2.  
    3.         MsgBox(url.Uri.ToString)

    If you pass the url to the constructor of a UriBuilder it will automagically escape the Query parameters.
    this gives you back the same string.. BUT I was able to get what I needed from UriBuilder(strUrl).Uri.AbsoluteUri

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