send utf-8 string http request

M

Mullin Yu

I want to submit a utf-8 xml request to a servlet by the following coding.
it seesm that the servlet can't recognize it correctly.

can i just using string postData = "..... utf-8 data" and then save the
files at utf-8 encoded at the vs.net ide?

or if i use string, vs.net ide will conside it to be utf-16???

UTF8Encoding encoding = new UTF8Encoding();


string postData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"urn:schemas-xmlsoap-org:envelope\">" +

"<SOAP-ENV:Header>" +

"<SOAPActionVersion>1.5.5</SOAPActionVersion></SOAP-ENV:Header>" +

"<SOAP-ENV:Body>" +

"<cmEDMSCustomerContactPage transactionType=\"UPDT\">" +

"<cmEDMSCustomerContactPageService>" +

"<cmEDMSCustomerContactPageHeader PageReadSW=\"false\"
CustomerContactID=\"8210710493\"></cmEDMSCustomerContactPageHeader>" +

"<cmEDMSCustomerContactPageDetails>" +

"<LogEntry>" +

"<LogEntryHeader CustomerContactID=\"8210710493\">" +

"</LogEntryHeader>" +

"<LogEntryRow rowAction=\"ADD\" CustomerContactID=\"8210710493\" LogEntry=\"
utf-8 characters \">" +

"</LogEntryRow>" +

"</LogEntry>" +

"</cmEDMSCustomerContactPageDetails>" +

"</cmEDMSCustomerContactPageService>" +

"</cmEDMSCustomerContactPage>" +

"</SOAP-ENV:Body >" +

"</SOAP-ENV:Envelope>";

Console.WriteLine(postData);


byte[] data = encoding.GetBytes(postData);


// Prepare web request...

HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create(http://172.18.2.1:7520/server);


myRequest.Method = "POST";

myRequest.ContentType="text/xml";

myRequest.ContentLength = data.Length;

myRequest.SendChunked = true;

myRequest.TransferEncoding = "UTF8";


myRequest.Credentials = new NetworkCredential("CDX", "pwd");

Stream newStream=myRequest.GetRequestStream();

// Send the data.

newStream.Write(data,0,data.Length);

newStream.Close();
 
J

Jon Skeet [C# MVP]

Mullin Yu said:
I want to submit a utf-8 xml request to a servlet by the following coding.
it seesm that the servlet can't recognize it correctly.

can i just using string postData = "..... utf-8 data" and then save the
files at utf-8 encoded at the vs.net ide?

or if i use string, vs.net ide will conside it to be utf-16???

UTF8Encoding encoding = new UTF8Encoding();

<snip>

There's no need to create a new UTF8Encoding - just use Encoding.UTF8.

The rest of your code looks almost correct though. All you need is to
change your content type so it's "text/xml; encoding=UTF-8" so that the
servlet can work out what you've sent it in. It should then be fine.
 
J

Joerg Jooss

Jon said:
Mullin Yu said:
I want to submit a utf-8 xml request to a servlet by the following
coding. it seesm that the servlet can't recognize it correctly.
[...]
There's no need to create a new UTF8Encoding - just use Encoding.UTF8.

Actually, there is. Encoding.UTF8 adds a BOM, which you definitely don't
want when posting data.

Cheers,
 
J

Joerg Jooss

Mullin said:
I want to submit a utf-8 xml request to a servlet by the following
coding. it seesm that the servlet can't recognize it correctly.

What's the exact error?
 
J

Jon Skeet [C# MVP]

Joerg Jooss said:
Jon said:
Mullin Yu said:
I want to submit a utf-8 xml request to a servlet by the following
coding. it seesm that the servlet can't recognize it correctly.
[...]
There's no need to create a new UTF8Encoding - just use Encoding.UTF8.

Actually, there is. Encoding.UTF8 adds a BOM, which you definitely don't
want when posting data.

I don't see any evidence of that when using Encoding.UTF8.GetBytes (as
the OP used):

using System;
using System.Text;

class Test
{
static void Main()
{
Console.WriteLine (Encoding.UTF8.GetBytes("x").Length);
}
}

prints 1, rather than 4 which it would print if a BOM were being used.

It uses a BOM if you create a StreamWriter with it though.

Even if you were to want to use a version of UTF8Encoding which didn't
use a BOM, I'd make it a static readonly member of another class rather
than creating a new instance every time you go through the routine.
 
M

Mullin Yu

the utf8 chinese character became rubblish at oracle db, but it's ok for the
java program, so that i suspect it's due to my limited knowledge of using
class and method in .net.

how about reading from a utf-8 file, i can read it, but it seems having 6
bytes of byte order marks. am i right?

thanks!
 
J

Jon Skeet [C# MVP]

Mullin Yu said:
the utf8 chinese character became rubblish at oracle db, but it's ok for the
java program, so that i suspect it's due to my limited knowledge of using
class and method in .net.

You need to find out exactly where the characters became "rubbish". The
way to do that is to output them (as unicode numbers rather than
glyphs) at various different points, and check where they go wrong.
how about reading from a utf-8 file, i can read it, but it seems having 6
bytes of byte order marks. am i right?

No, I believe there should only be 3 bytes of BOM: 0xef 0xbb 0xbf.
 
J

Joerg Jooss

Jon said:
Joerg Jooss said:
Jon said:
I want to submit a utf-8 xml request to a servlet by the following
coding. it seesm that the servlet can't recognize it correctly.
[...]
There's no need to create a new UTF8Encoding - just use
Encoding.UTF8.

Actually, there is. Encoding.UTF8 adds a BOM, which you definitely
don't want when posting data.

I don't see any evidence of that when using Encoding.UTF8.GetBytes (as
the OP used):

using System;
using System.Text;

class Test
{
static void Main()
{
Console.WriteLine (Encoding.UTF8.GetBytes("x").Length);
}
}

prints 1, rather than 4 which it would print if a BOM were being used.

It uses a BOM if you create a StreamWriter with it though.

Yeah, got that mixed up.
Even if you were to want to use a version of UTF8Encoding which didn't
use a BOM, I'd make it a static readonly member of another class
rather than creating a new instance every time you go through the
routine.

Sure, but tell it to the original author ;->
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top