Exemple #1
1
        public new IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
        {
            var result = new NetAsyncResult (state);

            xhr = new XmlHttpRequest ();


            xhr.ReadyStateChange += e =>
            {
                if (this.xhr.readyState == this.xhr.DONE) {
                    callback (result);
                }
            };

            if (Method == null)
            {
                Method = "GET";
            }

            xhr.Open (Method, this.uri.AbsoluteUri);

            if (this._headers != null) {
                for (int i = 0; i < this.Headers.Count; i++) {
                    var key = this.Headers.GetKey (i);
                    xhr.SetRequestHeader (key, this.Headers[key]);
                }
            }

             if (this._requestStream != null)
            {
                var sr = new System.IO.StreamReader (this._requestStream);
                var requestDataStr = sr.ReadToEnd();
                xhr.Send(requestDataStr);
            } else {
                xhr.Send ();
            }

            return result;
        }
        private static XmlDocument GetResponse(string soapXmlPacket, string action, Action <object> asyncCallback)
        {
            bool isAsync = (asyncCallback != null);

            string         xml            = getSoapEnvelope(soapXmlPacket);
            Exception      msg            = null;
            XmlHttpRequest xmlHttpRequest = new XmlHttpRequest();



            xmlHttpRequest.Open("POST", GetServerUrl() + "/XRMServices/2011/Organization.svc/web", isAsync);
            xmlHttpRequest.SetRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/" + action);
            xmlHttpRequest.SetRequestHeader("Content-Type", "text/xml; charset=utf-8");

            // This is only needed when debugging via localhost - and accessing the CRM webservices cross domain in chrome
            if (WithCredentials)
            {
                Script.Literal("{0}.withCredentials = true;", xmlHttpRequest);
            }

            if (isAsync)
            {
                xmlHttpRequest.OnReadyStateChange = delegate()
                {
                    if (xmlHttpRequest == null)
                    {
                        return;
                    }
                    if (xmlHttpRequest.ReadyState == ReadyState.Loaded)
                    {
                        // Capture the result
                        XmlDocument resultXml = xmlHttpRequest.ResponseXml;
                        Exception   errorMsg  = null;
                        if (xmlHttpRequest.Status != 200)
                        {
                            errorMsg = GetSoapFault(resultXml);
                        }
                        // Tidy Up
                        Script.Literal("delete {0}", xmlHttpRequest);

                        xmlHttpRequest = null;

                        if (errorMsg != null)
                        {
                            asyncCallback(errorMsg);
                        }
                        else
                        {
                            // Callback
                            asyncCallback(resultXml);
                        }
                    }
                };

                xmlHttpRequest.Send(xml);

                return(null);
            }
            else
            {
                xmlHttpRequest.Send(xml);

                // Capture the result
                XmlDocument resultXml = xmlHttpRequest.ResponseXml;

                // Check for errors.
                if (xmlHttpRequest.Status != 200)
                {
                    msg = GetSoapFault(resultXml);
                }
                // Tidy Up
                Script.Literal("delete {0};", xmlHttpRequest);
                xmlHttpRequest = null;

                if (msg != null)
                {
                    throw msg;
                }
                else
                {
                    // Return
                    return(resultXml);
                }
            }
        }