0

Am currently communicating to a Mobile device using Windows Compact Framework 3.5. The message sent to the device is built is as thus,

HttpResponseMessage result;
var response = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"windows-1252\"?><message type=\"response\"><header><datetime>2013-04-03T09:49:35</datetime><sender version=\"1.1.4.1138\"><userid>Connect Server</userid></sender><commandlist><module>ADMIN</module><command1>VALIDATE</command1></commandlist><result type=\"ok\"/></header></message>");

result = Request.CreateResponse(HttpStatusCode.OK, response);

The device then retrieves the message and then uses

Encoding.UTF8.GetString(responseContent);

After decoding the message is:

&lt;base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/"&gt;PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0id2luZG93cy0xMjUyIj8+PG1lc3NhZ2UgdHlwZT0icmVzcG9uc2UiPjxoZWFkZXI+PGRhdGV0aW1lPjIwMTMtMDQtMDNUMDk6NDk6MzU8L2RhdGV0aW1lPjxzZW5kZXIgdmVyc2lvbj0iMS4xLjQuMTEzOCI+PHVzZXJpZD5Db25uZWN0IFNlcnZlcjwvdXNlcmlkPjwvc2VuZGVyPjxjb21tYW5kbGlzdD48bW9kdWxlPkFETUlOPC9tb2R1bGU+PGNvbW1hbmQxPlZBTElEQVRFPC9jb21tYW5kMT48L2NvbW1hbmRsaXN0PjxyZXN1bHQgdHlwZT0ib2siLz48L2hlYWRlcj48L21lc3NhZ2U+&lt;/base64Binary&gt;

Tried decoding the message on the server before sending it off and it's fine. Unsure what could be going wrong.

Any help would be greatly appreciated.

maj21
  • 43
  • 2
  • 6

2 Answers2

2

Request.CreateResponse() uses ObjectContent. For this scenario, you don't want that. You should use either StringContent or StreamContent to return the XML. See this question for details https://stackoverflow.com/a/15372410/6819

Community
  • 1
  • 1
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
0

You are encoding your XML as binary. You are then returning a byte array. Then your client requests XML in the Accept: application/xml header. The Web API serializes the binary into XML. That's what you're seeing.

Just return the XML as a string and you should have no problems, unless you've tried that already?

See here for similar question.

Community
  • 1
  • 1
Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
  • That will have the same problem because Request.CreateResponse uses ObjectContent which assumes you want to use an object serializer. Returning a string will get an XML serialization of a string object, complete with seralization namespaces. – Darrel Miller Apr 05 '13 at 11:16
  • @DarrelMiller I expect you're right. That makes the accepted answer on the question I linked to wrong or misleading. – Tim Rogers Apr 05 '13 at 11:23
  • The wierd thing about the question you linked to is that the OP is using StringContent, so that should return just fine. The only problem is StringContent sets the content-type to text/plain so that needs to be overridden. But yes, the accepted answer is misleading. – Darrel Miller Apr 05 '13 at 11:35