private void HandleOutboundXmlRequest(IAsyncResult result) { // state... ODataRequestState state = (ODataRequestState)result.AsyncState; try { Stream stream = state.Request.EndGetRequestStream(result); if (stream == null) { throw new InvalidOperationException("'stream' is null."); } using (stream) { // send it... StreamWriter writer = new StreamWriter(stream); writer.Write(state.OutboundXml); writer.Flush(); } // ok... next... state.Request.BeginGetResponse(new AsyncCallback(HandleODataOperationResponse), state); } catch (Exception ex) { state.Failed(ex); } }
private void ExecuteODataOperation(ODataOperation opType, String url, String xml, Action callback, Failed failed) { // create the request... HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // set the method... if (opType == ODataOperation.Insert) { request.Method = "POST"; } else if (opType == ODataOperation.Update) { request.Method = "MERGE"; } else if (opType == ODataOperation.Delete) { request.Method = "DELETE"; } else { throw new NotSupportedException(string.Format("Cannot handle '{0}'.", opType)); } // headers... (including our special tokens)... DownloadSettings settings = this.GetDownloadSettings(); foreach (string name in settings.ExtraHeaders.Keys) { request.Headers[name] = settings.ExtraHeaders[name]; } // create a state object... ODataRequestState state = new ODataRequestState(); state.Request = request; state.Callback = callback; state.Failed = failed; state.OutboundXml = xml; // do we have xml? if (!(string.IsNullOrEmpty(xml))) { byte[] bs = Encoding.UTF8.GetBytes(xml); request.ContentType = "application/atom+xml"; request.BeginGetRequestStream(new AsyncCallback(HandleOutboundXmlRequest), state); } else { request.BeginGetResponse(new AsyncCallback(HandleODataOperationResponse), state); } }
private void HandleODataOperationResponse(IAsyncResult result) { // state... ODataRequestState state = (ODataRequestState)result.AsyncState; try { // unwrap... HttpWebResponse response = (HttpWebResponse)state.Request.EndGetResponse(result); if (response == null) { throw new InvalidOperationException("'response' is null."); } // dispose the response... response.Close(); // ok... state.Callback(); } catch (WebException ex) { StringBuilder builder = new StringBuilder(); builder.Append("An error occurred when making an OData request."); if (ex.Response is HttpWebResponse) { using (Stream stream = ((HttpWebResponse)ex.Response).GetResponseStream()) { StreamReader reader = new StreamReader(stream); builder.Append(reader.ReadToEnd()); } } // throw... throw new InvalidOperationException(builder.ToString(), ex); } catch (Exception ex) { state.Failed(ex); } }