.NET Framework - encoding xmlhttp response

Asked By Ghena on 13-Aug-07 04:22 AM
pXML = Server.URLencode("<?xml version=""1.0"" encoding=""iso-8859-1""?
Password><Language>"&lang&"</Language></Login>")
servletURL = "mysite"

dIM xmlDoc,xmlhttp
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
set xmlhttp = Server.CreateObject("Microsoft.XMLHTTP")

xmlhttp.open "POST", servletURL, false
xmlhttp.setRequestHeader "Content-type", "application/x-www-form-
urlencoded"
xmlhttp.send "pXML=" &pXML& "&pCall=Login"

If xmlhttp.status = 200 then
Response.write "risponde"
xmlDoc.loadXML(xmlhttp.ResponseText)
'xmlDoc.save server.mappath("/public/loginR_test.xml")

set sessione = xmlDoc.getElementsByTagName("IDSession")
response.write "<br>" & sessione(0).Text & "<br>"
else
Response.write "non risponde"
end if

My problems is about the response... and the encoding

session(0).Text must be an alphanumeric string only

but.. during the response. some characters (perhaps blankspace)
beacome ("+" or "/ " or  "|")
So this string is not correct for a new request to the webservice.

The support advice me to use an encode method as asp.net

Encoding iso = Encoding.GetEncoding("iso8859-1");
But really I dont know if asp contains a similar function...

I home in some further advices...
thanks in advance.




Anthony Jones replied on 13-Aug-07 06:18 PM
You really need to change the receiving page on 'mysite'.  Instead of
retreiving XML from a form field load it raw from the request object like
this:-

Dim oReq : Set oReq = Server.CreateObject("MSXML2.DOMDocument.3.0")
oReq.async = false
oReq.load Request

any other parameters you want to pass to 'mysite' use either a querystring
value or include in the XML itself.

Build your response in an XML DOM also (called oResp in example below) and
when complete use this code to send:-

Response.ContentType = "text/xml"
Response.CharSet = "UTF-8"
oResp.Save Response

Now you have a page that takes straight XML input and generates a straight
XML output with no encoding issues since.


Now your calling page code can look like this:-


Dim oLogon : Set oLogon = Server.CreateObject("MSXML2.DOMDocument.3.0")
oLogon.LoadXML "<Login><Username /><Password /><Language /></Login>"

With oLogon.documentElement.childNodes
.item(0) = "testuser"
.item(1) = "pwd"
.item(2) = lang
End With

servletURL = "mysite?pCall=Login"

Dim xmlDoc,xmlhttp

Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xmlhttp.open "POST", servletURL, false
xmlhttp.send oDOM

If xmlhttp.status = 200 then

Response.write "risponde"

Set xmlDoc = xmlhttp.ResponseXML

'Use SelectSingleNode and some XPath
set sessione = xmlDoc.getElementsByTagName("IDSession")
response.write "<br>" & sessione(0).Text & "<br>"
else
Response.write "non risponde"
end if

Points to note:-

a) version specific ProgIDs and that ServerXMLHTTP object is being used (do
not use XMLHTTP in a server app).

b) Don't build XML with string concatention, code above uses a compromise of
a template XML loaded with LoadXML then adding the changable dynamic data to
the DOM.

c) No xml declare or encoding in request XML leave it all using the default
UTF-8.

d) moved the pCall field to the query string

e) send method takes the request DOM as a parameter this causes xmlhttp to
use the correct encoding and to add the correct content type header.

f) since the page on 'mysite' specifies content-type as text/xml ResponseXML
contains the XML DOM.

g) since I don't know the structure of the response XML I can't tweak the
code further but I would use a specific XPath to retrieve IDSession.

h) Are you sure you want to post usernames and passwords in clear text like
this?




--
Anthony Jones - MVP ASP/ASP.NET
Ghena replied on 14-Aug-07 03:34 AM
Many thanks for your explanation.

I'm trying to approuch your method but it goes on error:

Microsoft VBScript runtime error '800a01c2'

Wrong number of arguments or invalid property assignment: 'item'

/inc/login_test.asp, line 13

below the page:

Dim oLogon : Set oLogon = Server.CreateObject("MSXML2.DOMDocument.
3.0")
oLogon.LoadXML "<Login><Username /><Password /><Language /></Login>"


With oLogon.documentElement.childNodes
.item(0) = "user"
.item(1) = "password"
.item(2) = "ENG"
End With


sub test()
dIM xmlDoc,xmlReq
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
XMLDoc.setProperty "SelectionLanguage","XPath"
XMLDoc.Async = False

Set xmlReq = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xmlReq.open "POST", servletURL, false
xmlReq.send Server.URLencode(oLogon)

If xmlReq.status = 200 then

Response.ContentType = "text/xml"
Response.CharSet = "UTF-8"
xmlDoc.Save Response
xmlDoc.loadXML(xmlReq.ResponseText)

set sessione = xmlDoc.SelectSingleNode("IDSession")
STRINGA = sessione(0).Text

else

Response.write "non risponde"

end if

end sub

call test()

-----------------------------------------------------------------------------
further,
Not all of your explanation is clear for example if I must to use:

xmlDoc.Save Response
xmlDoc.loadXML(xmlReq.ResponseText)

or

xmlDoc.Save Response
xmlDoc.loadXML(Response)

and about the request :
you wrote
xmlhttp.send oDOM

But the variable is oLogon
so it should be:
xmlReq.send Server.URLencode(oLogon)

could you coinfirm it ?

Thanks in advance
Anthony Jones replied on 14-Aug-07 04:52 PM
Sorry I didn't test it should be:-

With oLogon.documentElement.childNodes
.item(0).Text = "user"
.item(1).Text = "password"
.item(2).Text = "ENG"
End With


---

The above is code for the sending server to use, In my code it is:-

oResp.Save Response

It does not appear in your receiving page.


You don't need the abobe line at all anywhere.


Again not needed anywhere.



Yes it should be oLogon my bad typing again :(

No you should not URLencode it you should send it raw.

In my code it should've looked like:-

xmlhttp.send oLogon


--
Anthony Jones - MVP ASP/ASP.NET
Ghena replied on 15-Aug-07 07:18 AM
OK, Im tryed to do the approuch in this way:


sub test()
Dim oLogon : Set oLogon = Server.CreateObject("MSXML2.DOMDocument.
3.0")
oLogon.LoadXML "<Login><Username /><Password /><Language /></Login>"

With oLogon.documentElement.childNodes
.item(0).Text = "user"
.item(1).Text = "pwd"
.item(2).Text = "ITA"
End With

dIM xmlDoc,xmlReq
Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
XMLDoc.Async = False
XMLDoc.load Request

set xmlReq = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xmlReq.open "POST", servletURL, false
'xmlReq.send "pXML=" &pXML& "&pCall=Login"
xmlReq.send oLogon

If xmlReq.status = 200 then
Response.ContentType = "text/html"
Response.CharSet = "UTF-8"
xmlReq.Save Response

xmlDoc.save server.mappath("/public/loginR_test3.xml")
set sessione = xmlDoc.getElementsByTagName("IDSession")
STRINGA = sessione(0).Text

set xmldoc = Nothing
set xmlReq = NOthing
Set oLogon = Nothing
else

Response.write "non risponde"

end if
end sub

call test()


But I have a new error:

Microsoft VBScript runtime error '800a01b6'

Object doesn't support this property or method: 'xmlReq.Save'

THe second doubt is about the pCall parameter :
How can I add to the oLogon querystring ?

Thanks again...
Anthony Jones replied on 15-Aug-07 11:34 AM
xmlReq is a XMLHTTP object, it doesn't have a save.



Hmm.. I don't think I've made myself very clear.  There are two pages
involved in your task.  One that generates the XML which the URL for which
you are storing in the servletURL variable.  The other is the receiving
page.
What I'm suggesting to you is a change to both pages to ditch all the Forms
style URL encoding nonsense and to simply move the XML about in its native
raw UTF-8 encoded stream of characters.

Hence your generating page should look like this:-

Dim oReq : Set oReq = Server.CreateObject("MSXML2.DOMDocument.3.0")
oReq.async = false
oReq.load Request

Dim oResp: Set oResp = Server.CreateObject("MSXML2.DOMDocument.3.0")

' ------
' Put code here that will do what ever it is that uses thin input oReq
DOMand build the required output in the oResp DOM.

This code will use Request.QueryString("pCall") to find the value of pCall
and probably (now I'm really guessin here) look like this:-

Select Case Request.QueryString("pCall")
Case "Login"

' code that processes login XML and builds appropriate response in oResp.

Case "AnotherFunction"

' code for another function again use oReq as input and generating output in
oResp

Case "else"

oResp.AppendChild oResp.createElement("error")
oResp.documentElement.Text = "Call: " & Request.QueryString("pCall") & "
unknown"

End Select


'-----

Response.ContentType = "text/xml"
Response.CharSet = "UTF-8"
oResp.Save Response

Now you have page to which you can post your XML without the URL encoding
marlarky and that simple sends XML back.

Now that we've got a better generating page the consumer page is simpler:-

Dim oLogon : Set oLogon = Server.CreateObject("MSXML2.DOMDocument.3.0")
oLogon.LoadXML "<Login><Username /><Password /><Language /></Login>"

With oLogon.documentElement.childNodes
.item(0).Text = "testuser"
.item(1).Text = "pwd"
.item(2).Text = lang
End With

servletURL = "mysite?pCall=Login" ' Note querystring value

Dim xmlDoc,xmlhttp

Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xmlhttp.open "POST", servletURL, false
xmlhttp.send oLogon

If xmlhttp.status = 200 then

Response.write "risponde"

Set xmlDoc = xmlhttp.ResponseXML

'Use SelectSingleNode and some XPath
set sessione = xmlDoc.getElementsByTagName("IDSession")
response.write "<br>" & sessione(0).Text & "<br>"
else
Response.write "non risponde"
end if


--
Anthony Jones - MVP ASP/ASP.NET
Ghena replied on 16-Aug-07 07:02 AM
I tryed to use your method but the webservice return me an error as
the string is not correct...

In the documentation of the webservice is required the urlencode
method and the string it must to be send togherther with the


So I tryed again to solve in another method...


''''''PRODUZIONE'''''''''''
pXML = "<?xml version=""1.0"" encoding=""iso-8859-1""?
Password><Language>"&lang&"</Language></Login>"
servletURL = "http://mysite"
''''''''''''''''''''''''''

sub test()


dIM xmlDoc,xmlReq
Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
xmlDoc.SetProperty "SelectionLanguage", "XPath"
XMLDoc.Async = False

set xmlReq = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")

xmlReq.open "POST", servletURL, false
xmlReq.setRequestHeader "Content-Type", "application/x-www-form-
urlencoded"
xmlReq.setRequestHeader "CharSet", "UTF-8"
xmlReq.send "pXML=" &pXML& "&pCall=Login"
'xmlReq.send oLogon

If xmlReq.status = 200 then
Response.ContentType = "text/xml"
Response.CharSet = "UTF-8"
Set xmlDoc = xmlReq.ResponseXML
response.write  right(xmlDoc.selectSingleNode("Authentication/
IDSession").Text &vbcrlf,70)
end if

end sub

for i = 0 to 10
call test()
next

the results are:


hwUjRC3i72kR9BGNXSABB44HQAEENsaWVudFNlc3Npb25FSkI=
hwjq5BMCGbT4JBGNXSABB44XQAEENsaWVudFNlc3Npb25FSkI=
hwNZkbfogYHVlBGNXSABB44nQAEENsaWVudFNlc3Npb25FSkI=
hwmQy1IjH2zIVBGNXSABB443QAEENsaWVudFNlc3Npb25FSkI=
hwCczWv+ToOhRBGNXSABB45nQAEENsaWVudFNlc3Npb25FSkI=
(error string)
hwparHzaWN1XVBGNXSABB46HQAEENsaWVudFNlc3Npb25FSkI=
hwtYvf7rdeFzZBGNXSABB47HQAEENsaWVudFNlc3Npb25FSkI=
hwAot00S9Wf4lBGNXSABB48XQAEENsaWVudFNlc3Npb25FSkI=
hwKofaK7UqZCBBGNXSABB49HQAEENsaWVudFNlc3Npb25FSkI=
hwwL4gc4W5NMpBGNXSABB493QAEENsaWVudFNlc3Npb25FSkI=
hwqPvqO1pZp6hBGNXSABB4+nQAEENsaWVudFNlc3Npb25FSkI=
(error string)

As you can see the symbol (+) is present in 2 strings....
I really dont understand how solve it..