473,769 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Crea te(lcUrl);

string lcPostData =
"XMLData=" + HttpUtility.Url Encode("<Data>T est Data</Data>");

loHttp.Method=" POST";
loHttp.ContentT ype = "applicatio n/x-www-form-urlencoded";
byte [] lbPostBuffer = encoding.GetByt es(lcPostData);
loHttp.ContentL ength = lbPostBuffer.Le ngth;

Stream loPostData = loHttp.GetReque stStream();
loPostData.Writ e(lbPostBuffer, 0,lbPostBuffer. Length);
loPostData.Clos e();

HttpWebResponse loWebResponse = (HttpWebRespons e) loHttp.GetRespo nse();

Encoding enc = System.Text.Enc oding.GetEncodi ng(1252);

StreamReader loResponseStrea m =
new StreamReader(lo WebResponse.Get ResponseStream( ));

string lcHtml = loResponseStrea m.ReadToEnd();

Response.Write (lcHtml);

loWebResponse.C lose();
loResponseStrea m.Close();

Receiving code:
Stream str = Request.InputSt ream;
int strLen = (int)str.Length ;
byte[] bArr = new byte[strLen];
Int32 bytes = str.Read(bArr,0 ,strLen);
string strmContents = System.Text.Enc oding.ASCII.Get String(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.ContentT ype = "applicatio n/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.Enc oding.ASCII.Get String(bArr);
strXMLData = HttpUtility.Url Decode(strXMLDa ta);
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 1486
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.Crea te(lcUrl);

string lcPostData =
"XMLData=" + HttpUtility.Url Encode("<Data>T est Data</Data>");

loHttp.Method=" POST";
loHttp.ContentT ype = "applicatio n/x-www-form-urlencoded";
byte [] lbPostBuffer = encoding.GetByt es(lcPostData);
loHttp.ContentL ength = lbPostBuffer.Le ngth;

Stream loPostData = loHttp.GetReque stStream();
loPostData.Writ e(lbPostBuffer, 0,lbPostBuffer. Length);
loPostData.Clos e();

HttpWebResponse loWebResponse = (HttpWebRespons e)
loHttp.GetRespo nse();

Encoding enc = System.Text.Enc oding.GetEncodi ng(1252);

StreamReader loResponseStrea m =
new StreamReader(lo WebResponse.Get ResponseStream( ));

string lcHtml = loResponseStrea m.ReadToEnd();

Response.Write (lcHtml);

loWebResponse.C lose();
loResponseStrea m.Close();

Receiving code:
Stream str = Request.InputSt ream;
int strLen = (int)str.Length ;
byte[] bArr = new byte[strLen];
Int32 bytes = str.Read(bArr,0 ,strLen);
string strmContents = System.Text.Enc oding.ASCII.Get String(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.ContentT ype = "applicatio n/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.Enc oding.ASCII.Get String(bArr);
strXMLData = HttpUtility.Url Decode(strXMLDa ta);
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.d e
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.Crea te(lcUrl);

string lcPostData =
"XMLData=" + HttpUtility.Url Encode("<Data>T est Data</Data>");

loHttp.Method=" POST";
loHttp.ContentT ype = "applicatio n/x-www-form-urlencoded";
byte [] lbPostBuffer = encoding.GetByt es(lcPostData);
loHttp.ContentL ength = lbPostBuffer.Le ngth;

Stream loPostData = loHttp.GetReque stStream();
loPostData.Writ e(lbPostBuffer, 0,lbPostBuffer. Length);
loPostData.Clos e();

HttpWebResponse loWebResponse = (HttpWebRespons e)
loHttp.GetRespo nse();

Encoding enc = System.Text.Enc oding.GetEncodi ng(1252);

StreamReader loResponseStrea m =
new StreamReader(lo WebResponse.Get ResponseStream( ));

string lcHtml = loResponseStrea m.ReadToEnd();

Response.Write (lcHtml);

loWebResponse.C lose();
loResponseStrea m.Close();

Receiving code:
Stream str = Request.InputSt ream;
int strLen = (int)str.Length ;
byte[] bArr = new byte[strLen];
Int32 bytes = str.Read(bArr,0 ,strLen);
string strmContents = System.Text.Enc oding.ASCII.Get String(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.ContentT ype = "applicatio n/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.Enc oding.ASCII.Get String(bArr);
strXMLData = HttpUtility.Url Decode(strXMLDa ta);
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.d e

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.d e
Dec 12 '05 #7

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

Similar topics

1
2303
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 href="artist_page.php?12345678=<?php echo $row_rs_Q_Gender_to_Artist; ?>"><?php echo $row_rs_Q_Gender_to_Artist; ?></a>
10
687
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 codepage system wide. As much as it sounds good, UTF/Unicode encoding is not an option, nor is changing the codepage. Tackling the first is easy via Encoding.Default.GetBytes and shoving it over the network. However, Encoding.Default is the...
1
1488
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 encoding confusion. I finally settled on setting responseEncoding in my web.config file to "utf-16" and saving the output of the service encoded as "Unicode" (using Textpad where the options are "utf-8" and "Unicode") and everything was fine. This...
10
30198
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 backup files and it's a simple (yet extremely laborious!) process of opening each backup file in turn, reading the file line by line, splitting it up into its constituent parts, and then squirting it into SQL Server.
0
1972
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 tag = node.tag if tag is Comment: file.write("<!-- %s -->" % _escape_cdata(node.text, encoding)) elif tag is ProcessingInstruction: file.write("<?%s?>" % _escape_cdata(node.text, encoding))
19
3338
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" but somewhere in my code it got translated into the mess above and I cannot get the original string back. It cannot be printed in the console or written a plain text-file. I've tried to convert it using
1
32947
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); ###Question: Now this creates finalfile.txt with ANSI Encoding ...which is a default. Either tell me how to change the default or how to create a
17
2609
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 if it travelled in one byte. Why might this be? rj
12
3915
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 unambiguous how to interpret the character string in Bar? Does string.h expect a specific string format? void Foo(void)
0
9590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.