Results 1 to 7 of 7

Thread: [RESOLVED] Convert VB Url Encode Method to C#

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Resolved [RESOLVED] Convert VB Url Encode Method to C#

    Hi guys, I have a method that encodes a Url. I want to convert it to C# normally I dont have a issue with things like this but it looks like it is calling itself without a value passed, very strange looking. Any tips on how to convert are much appreciated.

    Code:
        Function URLEncode(ByVal Text As String) As String
    
            Dim i As Integer
            Dim acode As Integer
            'Dim char As String
    
            URLEncode = Text
    
            For i = Len(URLEncode) To 1 Step -1
                acode = Asc(Mid$(URLEncode, i, 1))
                Select Case acode
                    Case 48 To 57, 65 To 90, 97 To 122
                        ' don't touch alphanumeric chars
                    Case 32
                        URLEncode = URLEncode.Substring(0, i - 1) & "+" & Mid(URLEncode, i + 1)
                        ' replace space with "+"
                    Case Else
                        ' replace punctuation chars with "%hex"
                        URLEncode = URLEncode.Substring(0, i - 1) & "%" & Hex$(acode) & Mid$(URLEncode, i + 1)
                End Select
                ' End If
            Next
            If InStr(URLEncode, "%D%A") Then
                URLEncode = Replace(URLEncode, "%D%A", "%0D%0A")
            End If
        End Function

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Convert VB Url Encode Method to C#

    There's a built-in encode method already:

    System.Web.HttpServerUtility.UrlEncode(Text);
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: Convert VB Url Encode Method to C#

    Not in the compact framework there is not, or at least it doesnt seem to be.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: Convert VB Url Encode Method to C#

    Seems they changed it round:
    Code:
    System.Net.HttpUtility.UrlEncode(MessageToSend);

  5. #5
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: [RESOLVED] Convert VB Url Encode Method to C#

    OK, these two should be though:

    System.Uri.EscapeUriString(Text);
    System.Uri.EscapeDataString(Text);
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  6. #6
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Convert VB Url Encode Method to C#

    Quote Originally Posted by DeanMc View Post
    Seems they changed it round:
    Code:
    System.Net.HttpUtility.UrlEncode(MessageToSend);
    Good find!
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: [RESOLVED] Convert VB Url Encode Method to C#

    Cheers, it was a passing comment of a different site, nearly missed it.

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