2

I have a C# method that creates a request to a Java based web service. I serialize my class and post it to the service and it returns a status 400 indicating it can't find the named root element. When using fiddler I take the request and manually construct it in the Composer and process it and it succeeds. Using a hex viewer I notice that the request issued from C# has the following characters between the header and the request data EF BB BF. The cut and pasted request I executed in Fiddler does not and it succeeds. As I am not well versed in the construction of Java Services would it be easier to find a way to tell Java to ignore the characters when processing the request or would it be easier to build yet another serializer in .Net? It is not my intent to start a religious war here just to get to the fastest possible solution.

Three Ugly Characters Between Request And Response

The code to serialize looks like:

//seralize the Request
 XmlSerializer msgSerializer = new XmlSerializer(Msg.GetType());
  try
  {
      request = (HttpWebRequest)WebRequest.Create(_endpoint);
      request.Method = "POST";
      request.KeepAlive = true;
      request.SendChunked = true;
      using (Stream myRequestStream = request.GetRequestStream())
      {
            XmlTextWriter requestWriter = new XmlTextWriter(myRequestStream, Encoding.UTF8);
            msgSerializer.Serialize(requestWriter, Msg);
            myRequestStream.Close();
      }
      response = request.GetResponse();
  }
  catch (WebException webE)
  {
        //Yada Yada Yada
        ...
  }

1 Answer 1

2

That's a UTF-8 byte order mark. It's entirely valid for it to be there.

Frankly the Java code should cope with it, but the simplest way to fix it is probably to specify that you don't want it in the encoding:

XmlTextWriter requestWriter = new XmlTextWriter(myRequestStream,
    new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));

(If you're not using C# 4 or 5, just omit the encoderShouldEmitUTF8Identifier: part; that's a named argument, which makes it clearer what the argument value is there for.)

1
  • Thank You! Worked like a champ.
    – boydtwa
    Commented May 8, 2013 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.