Thanks for reaching out with this issue regarding string encoding when retrieving data from web in you VB application. The issue you are experiencing where characters like the accepted ‘á’ in ‘Villabáñez’ appear corrupted in your VB application but display correctly in a web browser is almost certainly an encoding mismatch.
When you use WC.DownloadString(strRequest), the System.Net.WebClient attempts to determine the character encoding of the response. By default , if the response headers don’t specify an encoding (like charset = utf – 8), WebClient often defaults to an encoding like ISO-8859-1 or a system default encoding (like Windows-1252 or ASCII). However, modern web services, especially those dealing with Spanish place names are most certainly sending the data encoded in UTF-8.
When WebClient interprets UTF-8 bytes as if the were ISO-8859-1, multi-byte characters like ‘á’ (which is two bytes in UTF-8) are incorrectly represented as two separate characters from the ISO-8859-1 set, resulting in the “mojibake” you see.
Solution: specify UTF-8 Encoding
The fix is to explicitly tell the WebClient to use UTF-8 encoding for the download string
Instead of using WC.DownloadString(strRequest), you should use WC.DownloadData to get raw bytes and then use the Encoding.UTF8 class to convert those bytes into a string.
Here’s the updated code of VB:
Using WC As New System.Net.WebClient
Try
1. Download the raw data (bytes) instead of a string Dim byteResponse() As Byte = WC.DownloadData(strRequest)
2. Explicitly convert the bytes to a string using UTF-8 encoding Dim strResponse As String= System.Text.Encoding.UTF8.GetString(byteResponse)
Now strResponse should contain ‘Villabáñez’ correctly
Continue with your existing logic XMLDoc.LoadXml(strResponse)
Catch ex As Exception
· Never Got Errors …
· Note: use a real error log or message box here in a production environment End Try
End Using
Why this works:
1. DownloadData: this method fetches the response content as a raw array of bytes (byteResponse()). This prevents the WebClient from attempting an incorrect default conversion.
2. System.Text.Encoding.UTF8.GetString(byteResponse): This is the critical step. It correctly decodes the sequence of the bytes using the UTF-8 standard, which preserves the special characters as intended by geocoding service.
Let me know if you need any further help with this. We'll be happy to assist.
If you find this helpful, please mark this as answered.