I realized this and got his job!
Although David's suggestions were not a solution in themselves, he put me on the right track by telling me to use a packet sniffer (I ended up using Wireshark, but thatβs not quite right).
As it turned out, there were two errors in my disabled code. One of the obvious things is that it makes me blush, a little more insidious.
Primarily,
using (var streamWriter = new StreamWriter(stream)) { streamWriter.Write(Encoding.UTF8.GetBytes(requestText)); }
should be of course
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8)) { streamWriter.Write(requestText); }
like streamWriter.Write uses ToString () for the parameter and Byte []. ToString () simply returns "System.Byte []". Offensive!
Secondly, the UTF8 encoding by default adds a byte order mark \ 357 \ 273 \ 277, which also makes the content invalid for google. I found how to solve this problem here on stackoverflow.
So, for those struggling with this, here is the final decision.
var url = string.Format ( "https://www.googleapis.com/calendar/v3/calendars?key={0}", application.Key ); var httpWebRequest = HttpWebRequest.Create(url) as HttpWebRequest; httpWebRequest.Headers["Authorization"] = string.Format("Bearer {0}", user.AccessToken.Token); httpWebRequest.Method = "POST"; // added the character set to the content-type as per David suggestion httpWebRequest.ContentType = "application/json; charset=UTF-8"; httpWebRequest.CookieContainer = new CookieContainer(); // replaced Environment.Newline by CRLF as per David suggestion var requestText = string.Join ( "\r\n", "{", " \"summary\": \"Test Calendar 123\"", "}" ); using (var stream = httpWebRequest.GetRequestStream()) // replaced Encoding.UTF8 by new UTF8Encoding(false) to avoid the byte order mark using (var streamWriter = new StreamWriter(stream, new UTF8Encoding(false))) { streamWriter.Write(requestText); }
Hope this helps someone!
source share