XMLHTTP.SetRequestHeader & Chinese encoding

Dear all,

I've a question about SetRequestHeader metod in Miscosoft.XMLHTTP.

In the following vb script in ASP file, my program is not function correctly if xmlMaster.xml includes Chinese characters.  I think the problem is due to incorrect character encoding of objHTTP.SetRequestHeader.

Is anybody know what parameter is required of SetRequestHeader if Chinese character includes in xmlMaster.xml?

Thanks.

==================================================

  Set objHTTP = CreateObject("Microsoft.XMLHTTP")
  objHTTP.Open "POST", "CBSTW1002_add_upd.asp?Action=DB", false
  objHTTP.SetRequestHeader "Content-Type" _
                         , "application/x-www-form-urlencoded"
  objHTTP.Send(xmlMaster.xml)
alan_cheungAsked:
Who is Participating?
 
BigRatCommented:
I see.

The problem is Send(xmlMaster.xml). xmlMaster.xml returns a BSTR containing UTF-16 bytes, so here Chinese is OK. But the Send method needs to convert this into a 8-bit bytes and URL encode them. I suspect that a UTF-16 to ISO-8898-1 transformation is being done, loosing all Chinese characters.

On an HTML <FORM> element you can specify the character set for transmission, although I have never been able to get this correct for all unicode characters. And I cannot see a method in XMLHTTP. So I adopted a different approach.

I scan the string to be sent (in this case xmlMaster.xml) for characters whose numerical value is greater than 128 and replace them with an entity sequence. I also convert the ampersand character as well. The "form" URL encodes this string and when I get it in my CGI program and URL unencode it I convert the entites back into UTF-16

My convert routine is :-

/* Function transfers Unicode text into 7-bit entities */
/* for text inputs and textareas */
function corrUnicode(hiddenId,textId) {
 var s=document.all(textId).value;
 document.all(hiddenId).value = s.replace (/(\w|\W)/g,
   function (s, n) {
     c=n.charCodeAt(0);
     return (c==38?'&amp;':(c>127?'&#'+c+';':n));
   }        
 );
}

and so I would then write :-

var s=xmlMaster.xml;
objHTTP.Send(s.replace (/(\w|\W)/g,
   function (s, n) {
     c=n.charCodeAt(0);
     return (c==38?'&amp;':(c>127?'&#'+c+';':n));
   });

At the other end you'd have to replace &amp; with Char(38) and &#nnn; with Char(nnn). I actually do this in C and convert to UFT-8 at the same time (but that's another matter)

HTH
0
 
BigRatCommented:
The following sends an XML docuent to the server :-

<script language="JScript">
  function PostOrder(xmldoc)
  {    
    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
    xmlhttp.Open("POST", "http://myserver/orders/processorder.asp", false);
    xmlhttp.Send(xmldoc);
  }
</script>

You don't need to set any RequestHeaders and you send the XML DOM Object with the Send method. (Not the text!)

When this arrives at the server :-

<%@ language=javascript %>
<%
    // Load the posted XML document.
    var doc = Server.CreateObject("Msxml2.DOMDocument.4.0");
    doc.load(Request);
   // HERE the document is in doc!
    var result = Server.CreateObject("Msxml2.DOMDocument.4.0");
    // Now process the order and build the result document.
    Response.ContentType = "text/xml";
    result.save(Response);
%>

(The code examples are extracted from MSDN) As far as I am aware the XML document is transfered transparently. There is no need to set any other parameters.

HTH
0
 
b1xml2Commented:
alan,

you can only do one of the following and not both.

1. Post XML Data To The Server
2. Post FORM Data To The Server VIA the POST HTTP Header.

You cannot mix the two up.

To Post XML DATA, you do not need to set any headers.

Set objHTTP = CreateObject("Microsoft.XMLHTTP")
'NO EXTRANEOUS DATA IN THE URL PLEASE
'THIS IS A POST AND NOT GET METHOD
objHTTP.Open "POST", "CBSTW1002_add_upd.asp", false
objHTTP.Send xmlMaster

To Post FORM DATA
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
'NO EXTRANEOUS DATA IN THE URL PLEASE
'THIS IS A POST AND NOT A GET METHOD
objHTTP.Open "POST", "CBSTW1002_add_upd.asp", false
objHTTP.SetRequestHeader "Content-Type",_
 "application/x-www-form-urlencoded"
objHTTP.SetRequestHeader "Content-Length", _
 Len(xmlMaster.xml)
objHTTP.Send(xmlMaster.xml)

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
b1xml2Commented:
Now, is there any reason why you cannot use the POST XML DATA instead of the FORM DATA as in
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q290591

The drawback on the client in posting FORM DATA is that the call MUST be asynchronous and encoding issues MAY be a problem.

Posting the XML DATA will protect your chinese encoding, provided you specify the correct encoding attribute in the xml pi.

My question to you alan is this: Why are you posting FORM DATA. If that is what you are doing, post it thru the traditional way via a IHTML FORM Element. If you want to post XML DATA, then do it as outlined by BigRat and the code provided by me.
0
 
alan_cheungAuthor Commented:
Thanks you all of your reply.

For the question raised by b1xml2, I need to posting FORM DATA because the xmlMaster in fact is a Data Island binded to the form.  When user click the Save button, the Data Island will be updated according to the form information.  Finally, a dll file will be called in CBSTW1002_add_upd.asp?Action=DB and information of xmlMaster will updated in DB.

For your reference, here is the sample of Data Island xmlMaster :

<xml id=xmlMaster>
<?xml version="1.0" encoding="BIG5">
<list>
  <row>
    <EngName>Alan</EngName>
    <ChiName>XXX</ChiName>
  </row>
</list>
</xml>


And the form binded with xmlMaster is as follow :

<Table width=550px>
  <tr>
    <td>
      <Fieldset>
        <Table Class=clsLabel01 DataSrc=#xmlMaster>

          <tr>
            <td></td>
            <td>English Name :</td>
            <td>
              <Input ID=txtEngName Class=clsInput type=text size=5 MaxLength=3
                     DataFld="EngName" style="text-transform: uppercase;">
            </td>
          </tr>

          <tr>
            <td></td>
            <td>Chinese Name :</td>
            <td>
              <Input ID=txtChiName Class=clsInput type=text size=5 MaxLength=3
                     DataFld="ChiName" style="text-transform: uppercase;">
            </td>
          </tr>

        </Table>
      </Fieldset>
    </td>
  </tr>

  <tr>
    <td align=right>
      <Button ID=cmdSave Accesskey=s Class=clsButton01><u>S</u>ave</Button>
      <Button ID=cmdCancel Accesskey=c Class=clsButton01><u>C</u>ancel</Button>
    </td>
  </tr>  
 
</Table>
0
 
alan_cheungAuthor Commented:
Thanks for BigRat details explaination.  I'll try your suggestion in this week end and see if it can solve my problem.  So, maybe reply and let u know if this solution work later.
Anyway, thanks for your help.
0
 
alan_cheungAuthor Commented:
Thank you HTH.  Your method is ok for my problem and now my program can work in the Chinese system.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.