private static void InvokeWebServiceRequest_Inner(IAsyncResult res) { WebInvokeServiceState state = res.AsyncState as WebInvokeServiceState; if (state == null) { throw new Exception("Web Service State not found"); } Stream stream = request.EndGetRequestStream(res); // serialize a request body if one was passed in (and the verb will take it) if (state.RequestBody != null && request.Method != "GET") { #if IOS request.UserAgent = UserAgents.IOSPhone; #else request.UserAgent = UserAgents.WinPhone; #endif // a null request body means that the caller wants to get the stream back and write to it directly if (state.RequestBody as Delegate != null) { Delegate streamDel = (Delegate)state.RequestBody; // invoke the delegate passed in with the request stream, so that the external caller // can push data into the stream as it becomes available streamDel.DynamicInvoke(stream); } else { // the caller passed the complete object - so just serialize it onto the stream if (state.RequestBody.GetType() == typeof(byte[])) { byte[] bytes = (byte[])state.RequestBody; #if !IOS stream = new GZipStream(stream, CompressionMode.Compress); request.ContentType = "application/x-gzip"; #else stream = new MemoryStream(); request.ContentType = "application/octet-stream"; #endif stream.Write(bytes, 0, bytes.Length); } else { request.ContentType = "application/json"; DataContractJsonSerializer ser = new DataContractJsonSerializer(state.RequestBody.GetType()); ser.WriteObject(stream, state.RequestBody); } // we got all the data into the stream, so flush/close it stream.Flush(); stream.Close(); } } // complete the invocation (this is not done inline because the InvokeWebServiceRequest_Inner_Complete() method // is reused by external callers that want to write to a stream directly and then invoke the operation) InvokeWebServiceRequest_Invoke(state.Delegate, state.NetworkOperationInProgressDelegate, state.Callback); }
private static void InvokeWebServiceRequest_Inner(IAsyncResult res) { WebInvokeServiceState state = res.AsyncState as WebInvokeServiceState; if (state == null) { TraceLog.TraceError("Web Service State not found"); return; } var request = state.Request; var netOpInProgressDel = state.NetworkOperationInProgressDelegate as Delegate; if (request == null) { TraceLog.TraceError("Web Service Request not found"); // signal the operation is done and unsuccessful if (netOpInProgressDel != null) { netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed); } return; } Stream stream = null; try { // this will throw if the connection can't be established stream = request.EndGetRequestStream(res); } catch (Exception ex) { TraceLog.TraceException("Can't obtain stream", ex); // signal the operation is done and unsuccessful if (netOpInProgressDel != null) { netOpInProgressDel.DynamicInvoke(false, OperationStatus.Failed); } return; } // serialize a request body if one was passed in (and the verb will take it) if (state.RequestBody != null && request.Method != "GET") { // a null request body means that the caller wants to get the stream back and write to it directly if (state.RequestBody as Delegate != null) { Delegate streamDel = (Delegate)state.RequestBody; // invoke the delegate passed in with the request stream, so that the external caller // can push data into the stream as it becomes available streamDel.DynamicInvoke(stream); } else { // the caller passed the complete object - so just serialize it onto the stream if (state.RequestBody.GetType() == typeof(byte[])) { /* * byte[] bytes = (byte[])state.RequestBody; #if !IOS * stream = new GZipStream(stream, CompressionMode.Compress); * request.ContentType = "application/x-gzip"; #else * stream = new MemoryStream(); * request.ContentType = "application/octet-stream"; #endif * stream.Write(bytes, 0, bytes.Length); */ } else { request.ContentType = "application/json"; DataContractJsonSerializer ser = new DataContractJsonSerializer(state.RequestBody.GetType()); ser.WriteObject(stream, state.RequestBody); } // we got all the data into the stream, so flush/close it stream.Flush(); stream.Close(); } } // complete the invocation (this is not done inline because the InvokeWebServiceRequest_Inner_Complete() method // is reused by external callers that want to write to a stream directly and then invoke the operation) InvokeWebServiceRequest_Invoke(request, state.Delegate, state.NetworkOperationInProgressDelegate, state.Callback); }