?
Solved

XMLHTTP.SetRequestHeader & Chinese encoding

Posted on 2002-06-05
7
Medium Priority
?
18,701 Views
Last Modified: 2012-05-04
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)
0
Comment
Question by:alan_cheung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 27

Expert Comment

by:BigRat
ID: 7056041
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 7058094
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 7058099
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:alan_cheung
ID: 7058614
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
 
LVL 27

Accepted Solution

by:
BigRat earned 300 total points
ID: 7058862
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
 

Author Comment

by:alan_cheung
ID: 7060003
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
 

Author Comment

by:alan_cheung
ID: 7072287
Thank you HTH.  Your method is ok for my problem and now my program can work in the Chinese system.
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question