Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1872
  • Last Modified:

MS ServerXMLHTTP or XMLHTTP Request

I'm using MSXML2 HTTP Object and trying to post an XML to a URL, when I post I first convert XML into string and use URL encode method and finally call send method of the object. I have some spanish characters in the XML, but when I get the response back the spanish characters are not recognized and the system puts a ? mark where ever the spanish characters are present, this b'cos the response XML is always encoded in UTF-8 format and UTF-8 does not recognize spanish characters. Could anyone help me how to retrieve data in iso-8859-1 format when i use XML HTTP object.
0
dureddyvv
Asked:
dureddyvv
  • 2
  • 2
  • 2
  • +3
1 Solution
 
edmund_mitchellCommented:
That's the magic word ->

Strings are always encoded as UTF-16 on the Win32 platform.  You have no choice in this matter, you cannot change it.
From the horses' mouth:
 "Use strings when you want to display output, not when you're just shuttling it to the next piece of a processing pipeline."

~Andy Kimball
MSXSL Dev

Other gotchas:
1. loadXML() takes a UTF-16 string as an argument
2. responseText returns the response converted to a UTF-16 string
3. transformNode() returns a UTF-16 string
4. Response.Write() takes a UTF-16 string as an argument.  It then magically converts the string to a byte stream encoded using the current session codepage.

The solution is to rewrite your code to avoid caching intermediate output in string form.  Here is an example from some ASP I had to do battle with:

    // Get response object
    //
    // responseXML returns a document created by parsing
    //the response stream, so no need to call load
    var xmlResponseDoc = xmlPostObject.responseXML;

    // Create stylesheet object
    //
    var xslFile = "whatever.xsl";
    var xslDoc = new ActiveXObject("MSXML2.DOMDocument");
    xslDoc.async = 0;
    xslDoc.load(Server.MapPath(xslFile));

    // Apply stylesheet to xml
    //
    // Do not allow intermediate result to be cached in
    // a string.
    // Instead, output directly to the ASP response
    // stream in order to
    // preserve the requested encoding.
    xmlResponseDoc.transformNodeToObject(xslDoc, Response);

I also recommend setting Response.Charset = "8859-1" as well as Response.Content="text/html" so that the browser can be quickly informed of the content and encoding of the incoming page (this avoids auto-detection logic).

HTH

Edmund


0
 
eyal_mtCommented:
make sure the you use the right codepage on the server - The first line should look some thing like this:

<%@ CODEPAGE=8859-1 Language=JavaScript %>

Then set the response charset:

Response.Charset = "8859-1"

And most important the header of your xml should include the encoding too, like this:

<?xml version='1.0' encoding='8859-1' ?>

good luck
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
dureddyvvAuthor Commented:
Thank you very much for your suggestions.
Well I'm not trying to display the return xml document to the browser.

Here is what exactly I have to do ..
I post a XML to a target url and i get a xml in response.
I have to save the response XML to a file and parse it to display only certain nodes/attributes.

Attached is the code I'm using to make a call and read the response...
Dim strResponseXML, xmlDoc, strXML
set xmldoc = Server.CreateObject("Msxml2.DOMDocument")

If xmldoc.loadXML(strXML) Then

     strResponseXML = mCallAllogis(xmldoc.xml)

End If
Function mCallAllogis(inXML)

Dim objinXMLdoc,objXMLHTTP, objResponsedoc
Dim strURL, strParameters, strinXML

set objinXMLdoc = Server.CreateObject("Msxml2.DOMDocument")
set objXMLHTTP     =
Server.CreateObject("Msxml2.ServerXMLHTTP")
set objResponsedoc = Server.CreateObject"Msxml2.DOMDocument")

strURL     = "http://localhost:81/http/postresponse.asp"
strParameters = "transactionName=processOrder&participant=Allogis&server=Allogis&message="

strinXML = strParameters + Server.URLEncode(strinXML)
objXMLHTTP.open "POST",strURL,False
objXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Response.ContentType = "application/xml"
Response.Charset = "1252"
objXMLHTTP.send strinXML

-- I HAVE TO READ THE RESPONSE HERE -----------....

End Function

The return XML is "iso-8859-1" encoded
As specified ealier , I have to read the XML in ASP and parse to extract only certain attributes/values and display them to the browser.
But when I read the Response the XML is not well formed do to special charcters.





0
 
anthony_glenwrightCommented:
FYI the MSXML2.ServerXMLHTTP object does not work on Win9x platforms, only Win2k/XP - the MSXML2.XMLHTTP object works on all of them, but is no good for server-class applications.
0
 
b1xml2Commented:
'test after sending
If objXMLHTTP.Status = 0 and objXMLHTTP.responseXML.parseError.errorCode = 0 Then
'to read the returned XML,
'first create the processing instruction with the appropriate encoding
oResult.appendChild oResult.createProcessingInstruction("xml","version='1.0' encoding='iso-8859-1'")
oResult.appendChild objXMLHTTP.documentElement
'now you have the correct syntax
End If

I had to do this for one project because using the responseText property caused the xml document to be either unloadable or the special accented characters were lost

Regards,

Brandon Driesen
0
 
MoondancerCommented:
It's time to clean up this topic area and that means taking care of this question. Your options at this point are:

1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.

2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

3.  Ask Community Support to help split points between participating experts.  Just comment here with details.

4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.

PLEASE DO NOT AWARD THE POINTS TO ME.
____________________________________________
---------->  Hi Experts:

In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.

------------->  The ALL TOPICS link has been updated today to reflect all the new TAs.

Thank you everyone.

Moondancer :)
Community Support Moderator @ Experts Exchange
0
 
b1xml2Commented:
<recommendation>
points to edmund_mitchell
</recommendation>
0
 
MoondancerCommented:
Thanks, b1xml2, for  your help here.

This has been finalized today, and monitoring for comments.

Moondancer - EE Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now