post data with HttpWebRequest to server

N

Natalie

Hy
I have a problem with sending a request to an ArcIMS server. The data
never reaches the server and I get the following error message:


...
AXLParser: Document cannot be parsed correctly. Check encoding.
...

Please have a look at my code and tell me what I'm doing wrong!
Thanks

code start-----------------------------------------------
httprequest = (HttpWebRequest) WebRequest.Create(target);
string request = "<ARCXML
version='1.1'><REQUEST><GET_IMAGE><PROPERTIES></PROPERTIES></GET_IMAGE><
/REQUEST></ARCXML>";
encoding = new UTF8Encoding();
byte[] byte1 = encoding.GetBytes(request); httprequest.ContentLength =
request.Length;
httprequest.Method = "POST";
httprequest.ContentType = "text/xml; encoding='utf-8'";
requestStream = httprequest.GetRequestStream();
requestStream.Write(byte1, 0, byte1.Length);
requestStream.Close();
code end--------------------------------------------------
 
J

Joerg Jooss

Natalie said:
Hy
I have a problem with sending a request to an ArcIMS server. The data
never reaches the server and I get the following error message:

The data certainly reaches the server, or where do you expect the error
message from the parser to come from ;-)
..
AXLParser: Document cannot be parsed correctly. Check encoding.
..

Please have a look at my code and tell me what I'm doing wrong!

Well, can't you debug or at least log on the server-side? The error message
is pretty obvious, although I can't see no good reason why your code should
fail -- maybe adding an XML declaration helps?

Cheers,
 
J

Jon Skeet [C# MVP]

Natalie said:
I have a problem with sending a request to an ArcIMS server. The data
never reaches the server and I get the following error message:
..
AXLParser: Document cannot be parsed correctly. Check encoding.
..

Please have a look at my code and tell me what I'm doing wrong!
Thanks

code start-----------------------------------------------
httprequest = (HttpWebRequest) WebRequest.Create(target);
string request = "<ARCXML
version='1.1'><REQUEST><GET_IMAGE><PROPERTIES></PROPERTIES></GET_IMAGE><
/REQUEST></ARCXML>";
encoding = new UTF8Encoding();
byte[] byte1 = encoding.GetBytes(request); httprequest.ContentLength =
request.Length;
httprequest.Method = "POST";
httprequest.ContentType = "text/xml; encoding='utf-8'";
requestStream = httprequest.GetRequestStream();
requestStream.Write(byte1, 0, byte1.Length);
requestStream.Close();
code end--------------------------------------------------

Well, the first problem is that you've set the content length to the
number of *characters* rather than the number of bytes. As it happens,
that's okay in your example as the characters will all be encoded in
one byte each, but it's not a good idea in general.

The second problem is that you've got utf-8 in quotes in the content
type, which I don't believe it's meant to be.

The third problem *may* be that your XML doesn't start with a
<?xml version="1" encoding="UTF-8" ?> tag - I believe the UTF-8 is
implicit, but not having it at all may confuse the parser.

I don't know whether or not it's okay to be utf-8 instead of UTF-8, but
I have experience of the latter working.
 

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

Top