My bad, Danny.
That should have read:
byte[] body = encoding.GetBytes("Xml=" +
HttpUtility.UrlEncode(XmlString));
I was translating it from some software I've written before, and made a
mistake in the translation.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
Hi Kevin
I tried what you said but get an error saying cannot implicitly convert
type
Byte[] to string,
string body = encoding.GetBytes("Xml=" +
HttpUtility.UrlEncode(XmlString));
I changed it the string body to byte [] and it worked fine when
Xml="sdfdsfsfsd", but when I put a "<" in the xml string an error
occurs :
The remote server returned an error: (500) Internal Server Error.
Can you see what could be causing this?
Thanks
Danny
:
Hi Danny,
If I understand you correctly, they are using a .Net program that
employs
the HttpWebRequest to send the XML data via HTTP POST?
If so, you need to create an HTML form on the fly so to speak. This is
not
too hard to do. An HTTP Post is a sequence of "name=value" pairs,
separated
by '&' characters, just like a QueryString, but longer. The "name"
part
is
the name of a form field, and the "value" part is the data for that
form
field. So, for example, if you wanted to POST a form having one field
named
"foo" and the value in it was "bar" your body would simply look like
this:
foo=bar
Of course, this must all be URLEncoded, and sending an XML document
means
a
lot of URLEncoding, for all the tags and such, but using URLEncode()
will
do
it. So, to send a single XML document via POST would be something
like:
string body = "Xml=" + HttpUtility.URLEncode(XmlString);
"Xml" would be the name of the form field.
It has to be encoded into a byte array because you are actually
sending a
stream, but it becomes a string at the other end. So, your code might
look
something like the following:
ASCIIEncoding encoding = new ASCIIEncoding();
Stream requestStream = null;
string lcUrl = "
http://localhost/test/receive.aspx";
HttpWebRequest IoHttp = (HttpWebRequest) WebRequest.Create(lcUrl);
IoHttp.Method = "POST";
IoHttp.ContentType = "application/x-www-form-urlencoded";
string body = encoding.GetBytes("Xml=" +
HttpUtility.URLEncode(XmlString));
IoHttp.ContentLength = body.Length;
try
{
requestStream = IoHttp.GetRequestStream();
requestStream.Write(body, 0, body.Length);
}
catch
{
//...
}
finally
{
if (requestStream != null) requestStream.Close();
}
In your ASPX page, you just get Request.Form["Xml"] which is already a
string.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition
Thanks Kevin
To receive the byte array from the client should I be using
Request.InputStream ?
Assuming the method I think the clients will use to send the data to
my
application, could be like this:
string lcUrl = "
http://localhost/test/receive.aspx";
HttpWebRequest loHttp =
(HttpWebRequest) WebRequest.Create(lcUrl);
// *** Send any POST data
string lcPostData =
"XMLData=" + HttpUtility.UrlEncode("xmldata..");
loHttp.Method="POST";
byte [] lbPostBuffer =
System.Text.Encoding.GetEncoding(1252).GetBytes(lcPostData);
loHttp.ContentLength = lbPostBuffer.Length;
:
Convert the byte array to a string. Create an XmlDocument instance,
and
use
the Load() method, passing the string to the constructor.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition
I am working on a project where I will receive xml documents from
clients
machines as a byte array. They will use the web browser navigate
method
to
post the data to my ASP.NET page. I then pick up the byte array
using
the
request object (XMLData=bytearray..). Can someone point me to an
article
that
shows me how I can do this in ASP.NET, C#?
Thanks
Danny