Random characters on a XMLHTTP post return

Status
Not open for further replies.

wookiecrisp

Ars Scholae Palatinae
891
I am using XMLHTTP to get the source code of a non-local web page. I then display it on my site.

However, I get these random ?'s all over the place, as if XMLHTTP cannot interpret some characters and uses ?'s instead. If I try to replace these ?'s using a regular expression, or whatever, they do not go away, no matter what, even though they are in the source code.

:(

Any one seen this before or have any solutions? Thanks.
 

Kurt

Ars Tribunus Angusticlavius
8,820
Subscriptor++
Are you turning the response into XML before you output it? The question mark is usually indicative of a character that isn't valid using the current XML Doctype. Is there any way to figure out exactly what is turning into a question mark? Also, if you view source on the page you're outputting, do the characters still appear as ? or are they something else?
 

wookiecrisp

Ars Scholae Palatinae
891
usually '?' seen in webpages is a sign of somebody using crappy Microsoft products
No... straight up coding, unless you count IIS as a crappy product.

the characters still appear as ? or are they something else?
Yup, still ?'s in source. Copy and paste into something else, they are still ?'s.

I'm thinking it might be an unrecognized character that XMLHTTP is bombing on, but as to why I can't replace those ?'s in returned source is beyond me.

Here is my code: (.asp)
Code:
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objXMLHTTP.Open "GET", strURL, False
objXMLHTTP.Send
strOutput = objXMLHTTP.responseText
Set objXMLHTTP = Nothing

strOutput is the source code of strURL, which has the "unrecognized characters'. Is there some content-type I need to set, or someway to get XMLHTTP to use Unicode or something?
 

wookiecrisp

Ars Scholae Palatinae
891
Ok, I know what the problem is, but I can't figure out a good solution.

The problem is that the strOutput = objXMLHTTP.responseText returns ASCII, so any unrecognized characters will come back as ?'s.

There is another method, objXMLHTTP.responseBody, that will return an array of character bytes, which you can then parse with a function like this:

Code:
Function Unicode(strResponseBody) 
   Dim i, t 
   t = String(LenB(strResponseBody), " ") 
   t = "" 
   For i = 1 To LenB(strResponseBody) 
      t = t & Chr(AscB(MidB(strResponseBody, i, 1))) 
   Next 
   Unicode = t 
End Function

However, this takes HELLA!!!!! long. So although a solution, it is not a solution. :(

Anyone have a more elegant solution?
 

technophile

Ars Legatus Legionis
23,768
Damn, I remember seeing a web page once upon a time that discussed the problems with AscB being slow, and how to do it quickly... can't recall what it was now. :(

Anyway, you could try StrConv:
StrConv
Purpose: Converts a string into specified format
Syntax: StrConv(strExpression, conversionType)
Arguments: strExpression - String to be converted
conversionType - Constant indicating conversion type

Constants:
Constant Value Desciption
vbUpperCase 1 Uppercases entire string
vbLowerCase 2 Lowercases entire string
vbProperCase 3 Uppercases first letter of each word
vbUnicode 64 Converts string to unicode using the default code page of the system
vbFromUnicode 128 Converts string from unicode to the default code page of the system

vbUnicode might do what you want... might not.
 

technophile

Ars Legatus Legionis
23,768
If that doesn't do it, or is still too slow, I found these guys here: http://www.eggheadcafe.com/articles/20010824.asp

Code:
Function RSBinaryToString(xBinary)
Dim Binary
If VarType(xBinary)=8 Then Binary = MultiByteToBinary(xBinary) Else Binary = xBinary
Dim RS, LBinary
Const adLongVarChar = 201
Set RS = CreateObject("ADODB.Recordset")
LBinary = LenB(Binary)
If LBinary>0 Then
RS.Fields.Append "mBinary", adLongVarChar, LBinary
RS.Open
RS.AddNew
RS("mBinary").AppendChunk Binary
RS.Update
RSBinaryToString = RS("mBinary")
Set RS=Nothing
Else
RSBinaryToString = ""
End If
Set RS=Nothing
End Function

Function MultiByteToBinary(MultiByte)
Dim RS, LMultiByte, Binary
Const adLongVarBinary = 205
Set RS = CreateObject("ADODB.Recordset")
LMultiByte = LenB(MultiByte)
If LMultiByte>0 Then
RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte
RS.Open
RS.AddNew
RS("mBinary").AppendChunk MultiByte & ChrB(0)
RS.Update
Binary = RS("mBinary").GetChunk(LMultiByte)
End If
Set RS = Nothing
MultiByteToBinary = Binary
End Function
 
Status
Not open for further replies.