encoding nightmare

G

Guest

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.Length);
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
 
J

Joerg Jooss

Danny said:
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.Length);
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,
 
G

Guest

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 said:
Danny said:
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.Length);
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,
 
K

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
 
G

Guest

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
 
K

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?
 
J

Joerg Jooss

Danny said:
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,
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top