[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
Re: Convert VB Url Encode Method to C#
There's a built-in encode method already:
System.Web.HttpServerUtility.UrlEncode(Text);
Re: Convert VB Url Encode Method to C#
Not in the compact framework there is not, or at least it doesnt seem to be.
Re: Convert VB Url Encode Method to C#
Seems they changed it round:
Code:
System.Net.HttpUtility.UrlEncode(MessageToSend);
Re: [RESOLVED] Convert VB Url Encode Method to C#
OK, these two should be though:
System.Uri.EscapeUriString(Text);
System.Uri.EscapeDataString(Text);
Re: Convert VB Url Encode Method to C#
Quote:
Originally Posted by
DeanMc
Seems they changed it round:
Code:
System.Net.HttpUtility.UrlEncode(MessageToSend);
Good find!
Re: [RESOLVED] Convert VB Url Encode Method to C#
Cheers, it was a passing comment of a different site, nearly missed it.