473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

encoding nightmare

I am working on a project in which a number of client applications will be
posting xml documents as a byte array to an ASP.NET page on our web server. I
am trying to simulate the process and run into problems.

Sending code:

ASCIIEncoding encoding = new ASCIIEncoding();

string lcUrl = "http://localhost/test/receive.aspx";
HttpWebRequest loHttp =
(HttpWebRequest) WebRequest.Create(lcUrl);

string lcPostData =
"XMLData=" + HttpUtility.UrlEncode("<Data>Test Data</Data>");

loHttp.Method="POST";
loHttp.ContentType = "application/x-www-form-urlencoded";
byte [] lbPostBuffer = encoding.GetBytes(lcPostData);
loHttp.ContentLength = lbPostBuffer.Length;

Stream loPostData = loHttp.GetRequestStream();
loPostData.Write(lbPostBuffer,0,lbPostBuffer.Lengt h);
loPostData.Close();

HttpWebResponse loWebResponse = (HttpWebResponse) loHttp.GetResponse();

Encoding enc = System.Text.Encoding.GetEncoding(1252);

StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream());

string lcHtml = loResponseStream.ReadToEnd();

Response.Write (lcHtml);

loWebResponse.Close();
loResponseStream.Close();

Receiving code:
Stream str = Request.InputStream;
int strLen = (int)str.Length;
byte[] bArr = new byte[strLen];
Int32 bytes = str.Read(bArr,0,strLen);
string strmContents = System.Text.Encoding.ASCII.GetString(bArr, 0,
bytes);
Response.Write (strmContents);
The above code will work with XMLData=xyz. But when “<” or “>” is in the
string as above, an error is returned from the receiving page:
The remote server returned an error: (500) Internal Server Error

If the following line is commented out, it works:
loHttp.ContentType = "application/x-www-form-urlencoded";

When it works with the above line commented out, the result displayed is:
XMLData=%3cData%3eTest+Data%3c%2fData%3e

If I use:
string strXMLData = System.Text.Encoding.ASCII.GetString(bArr);
strXMLData = HttpUtility.UrlDecode(strXMLData);
Response.Write (strXMLData);

The result I get is: XMLData=Test Data
I need the end result to be <Data>Test Data</Data>"

Where am I going wrong?

Thanks for any help
Dec 9 '05 #1
6 1468
Danny wrote:
I am working on a project in which a number of client applications
will be posting xml documents as a byte array to an ASP.NET page on
our web server. I am trying to simulate the process and run into
problems.

Sending code:

ASCIIEncoding encoding = new ASCIIEncoding();

string lcUrl = "http://localhost/test/receive.aspx";
HttpWebRequest loHttp =
(HttpWebRequest) WebRequest.Create(lcUrl);

string lcPostData =
"XMLData=" + HttpUtility.UrlEncode("<Data>Test Data</Data>");

loHttp.Method="POST";
loHttp.ContentType = "application/x-www-form-urlencoded";
byte [] lbPostBuffer = encoding.GetBytes(lcPostData);
loHttp.ContentLength = lbPostBuffer.Length;

Stream loPostData = loHttp.GetRequestStream();
loPostData.Write(lbPostBuffer,0,lbPostBuffer.Lengt h);
loPostData.Close();

HttpWebResponse loWebResponse = (HttpWebResponse)
loHttp.GetResponse();

Encoding enc = System.Text.Encoding.GetEncoding(1252);

StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream());

string lcHtml = loResponseStream.ReadToEnd();

Response.Write (lcHtml);

loWebResponse.Close();
loResponseStream.Close();

Receiving code:
Stream str = Request.InputStream;
int strLen = (int)str.Length;
byte[] bArr = new byte[strLen];
Int32 bytes = str.Read(bArr,0,strLen);
string strmContents = System.Text.Encoding.ASCII.GetString(bArr,
0, bytes);
Response.Write (strmContents);
The above code will work with XMLData=xyz. But when “<” or
“>” is in the string as above, an error is returned from the
receiving page: The remote server returned an error: (500) Internal
Server Error

If the following line is commented out, it works:
loHttp.ContentType = "application/x-www-form-urlencoded";

When it works with the above line commented out, the result displayed
is: XMLData=%3cData%3eTest+Data%3c%2fData%3e

If I use:
string strXMLData = System.Text.Encoding.ASCII.GetString(bArr);
strXMLData = HttpUtility.UrlDecode(strXMLData);
Response.Write (strXMLData);

The result I get is: XMLData=Test Data
I need the end result to be <Data>Test Data</Data>"

Where am I going wrong?


Hm... may I remind you of my previous answer (see
http://tinyurl.com/96dqn)?
Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Dec 9 '05 #2
Thanks for your answer, but I have no control over the client development, it
is being done by another company. I am trying to simulate the client, based
on how they have told me they will send the xml to my server, they said
"posting the xml data using the web browser navigate method, the xml will be
in format of a byte array prefixed with XMLData, which can be picked up with
the request object using ASP".
So you see why I am trying to simulate this. Any comments on this?

Thanks
Danny

"Joerg Jooss" wrote:
Danny wrote:
I am working on a project in which a number of client applications
will be posting xml documents as a byte array to an ASP.NET page on
our web server. I am trying to simulate the process and run into
problems.

Sending code:

ASCIIEncoding encoding = new ASCIIEncoding();

string lcUrl = "http://localhost/test/receive.aspx";
HttpWebRequest loHttp =
(HttpWebRequest) WebRequest.Create(lcUrl);

string lcPostData =
"XMLData=" + HttpUtility.UrlEncode("<Data>Test Data</Data>");

loHttp.Method="POST";
loHttp.ContentType = "application/x-www-form-urlencoded";
byte [] lbPostBuffer = encoding.GetBytes(lcPostData);
loHttp.ContentLength = lbPostBuffer.Length;

Stream loPostData = loHttp.GetRequestStream();
loPostData.Write(lbPostBuffer,0,lbPostBuffer.Lengt h);
loPostData.Close();

HttpWebResponse loWebResponse = (HttpWebResponse)
loHttp.GetResponse();

Encoding enc = System.Text.Encoding.GetEncoding(1252);

StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream());

string lcHtml = loResponseStream.ReadToEnd();

Response.Write (lcHtml);

loWebResponse.Close();
loResponseStream.Close();

Receiving code:
Stream str = Request.InputStream;
int strLen = (int)str.Length;
byte[] bArr = new byte[strLen];
Int32 bytes = str.Read(bArr,0,strLen);
string strmContents = System.Text.Encoding.ASCII.GetString(bArr,
0, bytes);
Response.Write (strmContents);
The above code will work with XMLData=xyz. But when “<” or
“>” is in the string as above, an error is returned from the
receiving page: The remote server returned an error: (500) Internal
Server Error

If the following line is commented out, it works:
loHttp.ContentType = "application/x-www-form-urlencoded";

When it works with the above line commented out, the result displayed
is: XMLData=%3cData%3eTest+Data%3c%2fData%3e

If I use:
string strXMLData = System.Text.Encoding.ASCII.GetString(bArr);
strXMLData = HttpUtility.UrlDecode(strXMLData);
Response.Write (strXMLData);

The result I get is: XMLData=Test Data
I need the end result to be <Data>Test Data</Data>"

Where am I going wrong?


Hm... may I remind you of my previous answer (see
http://tinyurl.com/96dqn)?
Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de

Dec 9 '05 #3
KJ
Do you know the MIME-type that the clients will be using (it seems you
do)?

Is it possible that in the posted data, they will be sending an
"entitized" version of the xml, to be decoded on your end? By this I
mean, perhaps they may be sending the XML chars encoded as &gt;, &lt;,
&apos;, and so on, to avoid the situation you describe.

Just an idea.

-KJ

Dec 9 '05 #4
They have not mentioned anything like that, the only info I really know is
the quote I gave above in my last post. Would sending the xml as a byte array
achieve what you are suggesting?

Thanks
Danny

"KJ" wrote:
Do you know the MIME-type that the clients will be using (it seems you
do)?

Is it possible that in the posted data, they will be sending an
"entitized" version of the xml, to be decoded on your end? By this I
mean, perhaps they may be sending the XML chars encoded as >, <,
&apos;, and so on, to avoid the situation you describe.

Just an idea.

-KJ

Dec 9 '05 #5
KJ
Well, I guess I am making the suggestion that if they entitized the xml
chars, the byte array would contain 4 (or more) chars instead of one,
i.e. &gt; instead of >. But the point I am making is that perhaps the
client has already run into the situation you are experiencing?

Dec 10 '05 #6
Danny wrote:
Thanks for your answer, but I have no control over the client
development, it is being done by another company. I am trying to
simulate the client, based on how they have told me they will send
the xml to my server, they said "posting the xml data using the web
browser navigate method, the xml will be in format of a byte array
prefixed with XMLData, which can be picked up with the request object
using ASP". So you see why I am trying to simulate this. Any
comments on this?


My other comment still applies. < and > are both rejected by ASP.NET's
request validation feature.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Dec 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Drisco | last post by:
Hello, I realy need help,Searched the NG for a days with no vail. Sorry for the long script. I am trying to get a variable from php to flash. I have the following code in php <a...
10
by: Christopher H. Laco | last post by:
Long story longer. I need to get web user input into a backend system that a) only grocks single byte encoding, b) expectes the data transer to be 1 bytes = 1 character, and c) uses the HP Roman-6...
1
by: Brad Wood | last post by:
I am testing a web service that returns an XML doc as a string. I went through troubles when developing it whereby the saved output of the service would not display in Internet Explorer due to...
10
by: Mark Rae | last post by:
Hi, I'm in the process if converting the data out of an old DOS-based SunAccounts system (don't ask!) into SQL Server. The data has been sent to me as a collection of hundreds of SunAccounts...
0
by: Chris McDonough | last post by:
ElementTree's XML serialization routine implied by tree._write(file, node, encoding, namespaces looks like this (elided): def _write(self, file, node, encoding, namespaces): # write XML to file...
19
by: Thomas W | last post by:
I'm getting really annoyed with python in regards to unicode/ascii-encoding problems. The string below is the encoding of the norwegian word "fdselsdag". I stored the string as "fdselsdag"...
1
by: ujjwaltrivedi | last post by:
Hey guys, Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); ...
17
by: raymond_b_jimenez | last post by:
I've seen a dump of the TDS traffic going from my webserver to the SQL Server database and it seems encoded in Unicode (it has two bytes per char). Seems it would have a huge impact on performance...
12
by: Andreas Lundgren | last post by:
Hi! Is it determined that the C standard compiler always encode characters with the same character excoding? If for example the functions Foo and Bar are compiled by different compilers, is it...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.