0

Performing a PUT to a client web service using this

using System;
using System.IO;
using System.Net;

class Test
{
    static void Main()
    {
        string xml = "<xml>...</xml>";
        byte[] arr = System.Text.Encoding.UTF8.GetBytes(xml);
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/");
        request.Method = "PUT";
        request.ContentType = "text/xml";
        request.ContentLength = arr.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(arr, 0, arr.Length);
        dataStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        string returnString = response.StatusCode.ToString();
        Console.WriteLine(returnString);
    }
}

Courtesy of this SO answer

If I look at the request in fiddler there is a strange character at the end of my post request </xml> looks like a square[suspect its a BOM]

not sure how it got introduced i my string.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1361914
  • 411
  • 1
  • 14
  • 26
  • The default `UTF8.GetBytes` does not include a BOM, and if you ask for one it is added to the front of the array not the end. The code you posted does not include a “strange character” in my test so something else is going on. Are you doing anything else to `arr`? Copy the “strange character” and look at it in a hex editor. – Dour High Arch Nov 22 '13 at 20:14
  • How you creating xml string? Example if you are using XmlWriter then you need to set like this: `new XmlWriterSettings { Encoding = new UTF8Encoding(false) }` – Jaex Nov 22 '13 at 20:25

1 Answers1

2

Use new UTF8Encoding(false).GetBytes(xml);

UTF8Encoding Constructor (Boolean): Initializes a new instance of the UTF8Encoding class. A parameter specifies whether to provide a Unicode byte order mark.

Jaex
  • 4,204
  • 2
  • 34
  • 56