public CreatePollResponse CreatePoll(PollType newPoll) { Debug.Assert(null != newPoll); // create a POST request WebRequest postRequest = WebRequest.Create(baseApiUrl + "/polls"); postRequest.ContentType = "application/xml"; postRequest.Method = "POST"; byte[] byteBuffer = serializePoll(newPoll); // set the content length before opening the stream postRequest.ContentLength = byteBuffer.Length; Stream postStream = postRequest.GetRequestStream(); postStream.Write(byteBuffer, 0, byteBuffer.Length); postStream.Close(); HttpWebResponse postResponse = null; try { // now we send the HTTP POST request postResponse = (HttpWebResponse)(postRequest.GetResponse()); } catch (Exception ex) { // bad HTTP requests will throw an exception } if ((null != postResponse) && (HttpStatusCode.Created == postResponse.StatusCode)) { // Successfully created .... now get the details string pollId = postResponse.Headers["Content-Location"]; string pollKey = postResponse.Headers["X-DoodleKey"]; string pollUrl = basePollUrl + "/" + pollId; return new CreatePollResponse { PollId = pollId, PollKey = pollKey, PollUrl = pollUrl }; } else { // Error of some form. TODO: throw appropriate exceptions return null; } }
// return content length private byte[] serializePoll(PollType poll) { System.IO.MemoryStream memStream = new System.IO.MemoryStream(); XmlTextWriter xtWriter = new XmlTextWriter(memStream, Encoding.UTF8); XmlSerializer xmlSerializer = new XmlSerializer(typeof(PollType)); xmlSerializer.Serialize(xtWriter, poll); xtWriter.Flush(); return memStream.ToArray(); }
/// <summary> /// returns an ID for the constructed Doodle poll /// </summary> /// <param name="request"></param> /// <returns></returns> private CreatePollResponse createDoodlePoll(ServiceRequest request) { PollType newPoll = new PollType(PollTypeType.TEXT, false, "Please answer this!", "2", request.Question); newPoll.initiator = new InitiatorType(); newPoll.initiator.name = "Praveen"; newPoll.options = new OptionsTypeOption[3]; newPoll.options[0] = new OptionsTypeOption(); newPoll.options[0].Value = "Three?"; newPoll.options[1] = new OptionsTypeOption(); newPoll.options[1].Value = "Four?"; newPoll.options[2] = new OptionsTypeOption(); newPoll.options[2].Value = "Five?"; CreatePollResponse createPollResponse = doodleClient.CreatePoll(newPoll); return createPollResponse; ; }