PC Review


Reply
Thread Tools Rate Thread

Stream.Write / Encoding problem

 
 
Ireney Berezniak
Guest
Posts: n/a
 
      25th Nov 2004
Hi, I am trying to send an UTF-8 XML string to remote resource via
HttpWebRequest using POST method, which means that I need to get the
request stream and write my XML string to it. No problems here.

The problem that I'm running into is that the "+" sign that one of the
XML elements contains is lost somehow in the transaction. I verified
that the "+" is actually in the XML string prior to getting bytes, and
writing to stream:

byte[] data;
UTF8Encoding encoding = new UTF8Encoding();

data = encoding.GetBytes(formData) //formData is a string containg XML

//here request is created
....

//now add the form data to the request
//first, get the request stream
Stream stream = request.GetRequestStream();

//write data to the stream
stream.Write(data, 0, data.Length);

Any ideas why the "+" sign is lost? If I convert the byte array (data)
back into string, the "+" is there. The remote resource does not get
it, however.

If I send the same XML via reqular web form post, rather than using the
server-side HttpWebRequest call, everything works fine. This leads me
to believe that I am not doing something properly as far as encoding is
concerned. I would appreciate any pointers.

This is how the XML is constructed. I'm concatonating the string
essentially, inserting data I collected from web from into placeholders
at later time.

private string GetXMLRequest()
{
string xml = String.Empty;

xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
xml += "<TranxRequest>";
xml += "<MerchantNumber>{0}</MerchantNumber>";
xml += "<Products>{1}</Products>";
xml += "<xxxName>{2}</xxxName>";
xml += "<xxxCompany>{3}</xxxCompany>";
xml += "<xxxAddress>{4}</xxxAddress>";
xml += "<xxxCity>{5}</xxxCity>";
xml += "<xxxProvince>{6}</xxxProvince>";
xml += "<xxxPostal>{7}</xxxPostal>";
xml += "<xxxCountry>{8}</xxxCountry>";
xml += "<xxxPhone>{9}</xxxPhone>";
xml += "<xxxEmail>{10}</xxxEmail>";
xml += "<xxxCard_Number>{11}</xxxCard_Number>";
xml += "<xxxCCMonth>{12}</xxxCCMonth>";
xml += "<xxxCCYear>{13}</xxxCCYear>";
xml += "<CVV2Indicator>{14}</CVV2Indicator>";
xml += "<CVV2>{15}</CVV2>";
xml += "<xxxShippingName>{16}</xxxShippingName>";
xml += "<xxxShippingCompany>{17}</xxxShippingCompany>";
xml += "<xxxShippingAddress>{18}</xxxShippingAddress>";
xml += "<xxxShippingCity>{19}</xxxShippingCity>";
xml += "<xxxShippingPostal>{20}</xxxShippingPostal>";
xml += "<xxxShippingCountry>{21}</xxxShippingCountry>";
xml += "<xxxShippingPhone>{22}</xxxShippingPhone>";
xml += "<xxxShippingEmail>{23}</xxxShippingEmail>";
xml += "</TranxRequest>";

return xml;
}

ib.
 
Reply With Quote
 
 
 
 
William Stacey [MVP]
Guest
Posts: n/a
 
      25th Nov 2004
I would test converting xml string to base64 and sending that then
converting back from base64 at the server. That way you may be able to
figure out if it is the http webrequest that is chopping the "+" or
something. If the base64 works, then probably something weird with http web
request and you at least has a starting point. Also, you may just want to
use XmlSerializer to serialize your class to reduce your maintaince and
chances of making xml error using your built up approach. XmlSerializer
perf will probably not be an issue for your app as this looks like one
request and response. hth.

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Ireney Berezniak" <(E-Mail Removed)> wrote in message
news:v_apd.1510$VL6.208@clgrps13...
> Hi, I am trying to send an UTF-8 XML string to remote resource via
> HttpWebRequest using POST method, which means that I need to get the
> request stream and write my XML string to it. No problems here.
>
> The problem that I'm running into is that the "+" sign that one of the
> XML elements contains is lost somehow in the transaction. I verified
> that the "+" is actually in the XML string prior to getting bytes, and
> writing to stream:
>
> byte[] data;
> UTF8Encoding encoding = new UTF8Encoding();
>
> data = encoding.GetBytes(formData) //formData is a string containg XML
>
> //here request is created
> ...
>
> //now add the form data to the request
> //first, get the request stream
> Stream stream = request.GetRequestStream();
>
> //write data to the stream
> stream.Write(data, 0, data.Length);
>
> Any ideas why the "+" sign is lost? If I convert the byte array (data)
> back into string, the "+" is there. The remote resource does not get
> it, however.
>
> If I send the same XML via reqular web form post, rather than using the
> server-side HttpWebRequest call, everything works fine. This leads me
> to believe that I am not doing something properly as far as encoding is
> concerned. I would appreciate any pointers.
>
> This is how the XML is constructed. I'm concatonating the string
> essentially, inserting data I collected from web from into placeholders
> at later time.
>
> private string GetXMLRequest()
> {
> string xml = String.Empty;
>
> xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
> xml += "<TranxRequest>";
> xml += "<MerchantNumber>{0}</MerchantNumber>";
> xml += "<Products>{1}</Products>";
> xml += "<xxxName>{2}</xxxName>";
> xml += "<xxxCompany>{3}</xxxCompany>";
> xml += "<xxxAddress>{4}</xxxAddress>";
> xml += "<xxxCity>{5}</xxxCity>";
> xml += "<xxxProvince>{6}</xxxProvince>";
> xml += "<xxxPostal>{7}</xxxPostal>";
> xml += "<xxxCountry>{8}</xxxCountry>";
> xml += "<xxxPhone>{9}</xxxPhone>";
> xml += "<xxxEmail>{10}</xxxEmail>";
> xml += "<xxxCard_Number>{11}</xxxCard_Number>";
> xml += "<xxxCCMonth>{12}</xxxCCMonth>";
> xml += "<xxxCCYear>{13}</xxxCCYear>";
> xml += "<CVV2Indicator>{14}</CVV2Indicator>";
> xml += "<CVV2>{15}</CVV2>";
> xml += "<xxxShippingName>{16}</xxxShippingName>";
> xml += "<xxxShippingCompany>{17}</xxxShippingCompany>";
> xml += "<xxxShippingAddress>{18}</xxxShippingAddress>";
> xml += "<xxxShippingCity>{19}</xxxShippingCity>";
> xml += "<xxxShippingPostal>{20}</xxxShippingPostal>";
> xml += "<xxxShippingCountry>{21}</xxxShippingCountry>";
> xml += "<xxxShippingPhone>{22}</xxxShippingPhone>";
> xml += "<xxxShippingEmail>{23}</xxxShippingEmail>";
> xml += "</TranxRequest>";
>
> return xml;
> }
>
> ib.


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect the encoding of a stream Roshan Microsoft Dot NET Framework 1 19th Dec 2005 11:43 AM
when i use stream writer to write to a shared folder, and the network card of that remote computer is disabled in the middle of the write Daniel Microsoft Dot NET Framework 0 8th Sep 2005 02:31 AM
Stream.Write / encoding problem? Ireney Berezniak Microsoft ASP .NET 0 24th Nov 2004 07:21 PM
Using Stream objects with encoding Gaia C via .NET 247 Microsoft C# .NET 3 6th Sep 2004 12:21 PM
How can i get one stream Encoding Name? Help me? qushui_chen Microsoft C# .NET 0 21st Oct 2003 04:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:08 AM.