Stream.Write / Encoding problem

  • Thread starter Thread starter Ireney Berezniak
  • Start date Start date
I

Ireney Berezniak

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.
 
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 said:
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.
 
Back
Top