Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Questions & Help Needed
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Questions & Help Needed Post any question regarding Basic4ppc.


HTTP Post fails if Umlauts sent and UTF-8 is used


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-28-2008, 11:11 AM
Knows the basics
 
Join Date: Apr 2008
Location: Duesseldorf, Germany
Posts: 71
Default HTTP Post fails if Umlauts sent and UTF-8 is used

Hello,

i got another one:

The code below works fine, but as soon as "mystring" contains Umlauts, an error message complaining about length mismatch occurs.


Quote:
Request.ContentLength = StrLength("mystring")
...
stream.New1(Request.GetStream,false)
...
stream.WriteBytes (stream.StringToBytes("mystring" ))
Error description:
Bytes to be written to the stream exceed the Content-Length size specified.



stream is a binary file object.
Request is a webrequest.


Accordingly to the Helpfile, the binaryfile.new1( , false or true) is set
to true for ASCII and to false for UTF-8:

Quote:
ASCII - If false, strings will be encoded using UTF-8 (Unicode) format, otherwise strings will be encoded using ASCII format.

If i set this to true, no error message occurs ( because there's no mismatch between stream length and content-length anymore), but the Umlauts are coverted to something like "?".So ASCII does not help here.



regards

TWELVE
Reply With Quote
  #2 (permalink)  
Old 04-28-2008, 12:24 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,335
Default

Can you post a simple text you are trying to send?
Reply With Quote
  #3 (permalink)  
Old 04-28-2008, 12:35 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,900
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

Quote:
Originally Posted by TWELVE View Post
The code below works fine, but as soon as "mystring" contains Umlauts, an error message complaining about length mismatch occurs.
This is because the umlauts are converted to 2 byte characters in UTF8 so lengthening the string. Try converting to a buffer then using the buffer size like this -

Code:
Sub Globals
      Dim buffer(0) As Byte
End Sub


Sub App_Start

  buffer() = stream.StringToBytes("mystring")
  Request.ContentLength = ArrayLen(buffer()) 
  ...
  stream.New1(Request.GetStream,false)
  ...
  stream.WriteBytes(buffer()) 

End Sub
Reply With Quote
  #4 (permalink)  
Old 04-28-2008, 12:41 PM
Knows the basics
 
Join Date: Apr 2008
Location: Duesseldorf, Germany
Posts: 71
Default

Quote:
Can you post a simple text you are trying to send?
Sure...Umlauts are Ä,Ö and Ü ( lower case ä,ö,ü).

"Ich fahre nach Österreich"

kind regards

TWELVE
Reply With Quote
  #5 (permalink)  
Old 04-28-2008, 01:47 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,335
Default

As Agraham wrote you should use the buffer size and not the string size.
You will need to add a Bitwise object:
Code:
request.New1(...)
    request.Method = "POST"
    request.ContentType = "application/x-www-form-urlencoded"
    bitwise.New1
    s = "Ich fahre nach Österreich"
    buffer() = bitwise.StringToBytes(s,0,StrLength(s))
    request.ContentLength = ArrayLen(buffer())
    stream.New1(request.GetStream,true)
    stream.WriteBytes(buffer())
    response.New1
    response.Value = request.GetResponse
    textbox1.Text = response.GetString
Reply With Quote
  #6 (permalink)  
Old 04-28-2008, 02:18 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,900
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

Silly me, I used the stream before it was opened But out of interest do you actually need the Bitwise object? Would this work?
Code:
    request.New1(...)
    request.Method = "POST"
    request.ContentType = "application/x-www-form-urlencoded"
    s = "Ich fahre nach Österreich"
    stream.New1(request.GetStream,false) ' UTF8 encoding
    buffer() = stream.StringToBytes(s) ' use the stream to convert
    request.ContentLength = ArrayLen(buffer())
    stream.WriteBytes(buffer())
    response.New1
    response.Value = request.GetResponse
    textbox1.Text = response.GetString
There are some subtleties here that I don't understand Why does Erel's example open the stream as ASCII? And does the Bitwise StringToBytes method return UTF8 formatted bytes/characters
Reply With Quote
  #7 (permalink)  
Old 04-28-2008, 02:27 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,335
Default

Quote:
does the Bitwise StringToBytes method return UTF8 formatted bytes/characters
Bitwise.New1 uses the default UTF8 encoding.
Bitwise.New2 allows you to choose other encodings.

Quote:
Why does Erel's example open the stream as ASCII?
As we are using the stream just to write bytes and not strings it doesn't matter whether we open it as ASCII or UTF8. The encoding only matters when reading or writing strings.

request.ContentLength must be set before request.GetStream. Otherwise you will get an error.
Reply With Quote
  #8 (permalink)  
Old 04-28-2008, 02:38 PM
Knows the basics
 
Join Date: Apr 2008
Location: Duesseldorf, Germany
Posts: 71
Default

Hello,

i solved this issue now by using:

Quote:
stream.New2(Request.GetStream,1252)
instead of using:

Quote:
stream.New1(Request.GetStream,false|true)
After some research and trials i found the code page 1252 to be appropriate for the german umlauts.


Quote:
This is because the umlauts are converted to 2 byte characters in UTF8 so lengthening the string. Try converting to a buffer then using the buffer size like this -
I do understand the difference between the count of characters and the count of bytes they allocate.I didn't know how to get that calculated properly and clean...thx to agraham and Erel for your code examples.

Usually Unicode or UTF-8 is they better choice for supporting different languages / characters.For the moment i will stay with the code page, since this is the quicker fix for me ( the webserver also need an additional module for UTF-8 support..)

@Erel: is it possible to implement something like "STRByteLength(string)" to have the bytes of the string counted instead of the characters..? Or maybe as additional parameter in STRLength(string, mode)...?


kind regards

TWELVE

Last edited by TWELVE : 04-28-2008 at 02:40 PM.
Reply With Quote
  #9 (permalink)  
Old 04-28-2008, 02:42 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,335
Default

Quote:
@Erel: is it possible to implement something like "STRByteLength(string)" to have the bytes of the string counted instead of the characters..?
The length depends on the encoding used.
As agraham and I wrote in previous posts you can measure it by first converting the string to bytes (using a specific encoder/code page).
Reply With Quote
  #10 (permalink)  
Old 04-28-2008, 02:47 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,900
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

Hmm! Thanks Erel, it is times like this when you realise how little you really know about some things - like character codings

Quote:
Originally Posted by TWELVE View Post
@Erel: is it possible to implement something like "STRByteLength(string)" to have the bytes of the string counted instead of the characters..? Or maybe as additional parameter in STRLength(string, mode)...?
A case for the Door library Erel?
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTP POST error message miataman Questions & Help Needed 10 07-11-2008 11:28 AM
HTTP POST error Elrick Bug Reports 3 05-24-2008 03:06 PM
HTTP POST on Device - error message TWELVE Questions & Help Needed 6 04-26-2008 10:35 AM
HTTP Post nsidney Questions & Help Needed 4 01-30-2008 10:52 AM
HTTP Post ohkovar Questions & Help Needed 3 07-07-2007 05:36 PM


All times are GMT. The time now is 06:04 PM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0