Example #1
0
        // Serialise objects to XML and POST to the Clockwork API
        private void XmlPost <T, U>(string url, ref T send, ref U response)
        {
            if (String.IsNullOrEmpty(url.Trim()))
            {
                throw new ArgumentException("URL Needed for POST", "url");
            }

            if (SSL)
            {
                url = "https://" + url;
            }
            else
            {
                url = "http://" + url;
            }

            UTF8StringWriter outStream  = new UTF8StringWriter();
            XmlSerializer    serializer = new XmlSerializer(typeof(T));

            serializer.Serialize(outStream, send);
            string req = outStream.ToString();

            outStream.Dispose();

            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);

            httpReq.UserAgent        = "Clockwork .NET Wrapper/" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            httpReq.Timeout          = 60000;
            httpReq.ReadWriteTimeout = 60000;
            httpReq.Method           = "POST";
            httpReq.ContentLength    = System.Text.Encoding.UTF8.GetByteCount(req);
            httpReq.ContentType      = "text/xml; charset=utf-8";
            if (Proxy != null)
            {
                httpReq.Proxy = Proxy;
            }

            StreamWriter myStream = new StreamWriter(httpReq.GetRequestStream());

            myStream.Write(req);
            myStream.Close();

            HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

            if (((HttpWebResponse)httpResp).StatusCode == HttpStatusCode.OK)
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(U));
                response = (U)deserializer.Deserialize(httpResp.GetResponseStream());
            }
            httpResp.Close();
        }
Example #2
0
        // Serialise objects to XML and POST to the Clockwork API
        private void XmlPost <TS, TR>(string url, ref TS send, ref TR response)
        {
            if (string.IsNullOrEmpty(url.Trim()))
            {
                throw new ArgumentException("URL Needed for POST", nameof(url));
            }

            var outStream  = new UTF8StringWriter();
            var serializer = new XmlSerializer(typeof(TS));

            serializer.Serialize(outStream, send);
            var req = outStream.ToString();

            outStream.Dispose();


#if PCL
            try
            {
                var clientHandler = new HttpClientHandler
                {
                    Proxy      = Proxy,
                    UseProxy   = Proxy != null,
                    UseCookies = false
                };
                using (var client = new HttpClient(clientHandler))
                {
                    client.BaseAddress = new Uri((SSL ? "https://" : "http://") + BaseUrl);
                    client.DefaultRequestHeaders.Accept.Clear();
                    var result = client.PostAsync(url, new StringContent(req, Encoding.UTF8)).Result;
                    result.EnsureSuccessStatusCode();
                    {
                        var responseStream = result.Content.ReadAsStreamAsync().Result;

                        var deserializer = new XmlSerializer(typeof(TR));
                        if (responseStream == null)
                        {
                            throw new WebException("Blank response");
                        }
                        response = (TR)deserializer.Deserialize(responseStream);
                    }
                }
            }
            catch (AggregateException exception)
            {
                throw exception.Flatten().InnerExceptions.FirstOrDefault();
            }
#else
            if (SSL)
            {
                url = "https://" + BaseUrl + url;
            }
            else
            {
                url = "http://" + BaseUrl + url;
            }


            var httpReq = (HttpWebRequest)WebRequest.Create(url);
            httpReq.UserAgent        = "Clockwork .NET Wrapper/" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            httpReq.Timeout          = 60000;
            httpReq.ReadWriteTimeout = 60000;
            httpReq.Method           = "POST";
            httpReq.ContentLength    = System.Text.Encoding.UTF8.GetByteCount(req);
            httpReq.ContentType      = "text/xml; charset=utf-8";
            if (Proxy != null)
            {
                httpReq.Proxy = Proxy;
            }

            var myStream = new StreamWriter(httpReq.GetRequestStream());
            myStream.Write(req);
            myStream.Close();

            var httpResp = (HttpWebResponse)httpReq.GetResponse();
            if (httpResp.StatusCode == HttpStatusCode.OK)
            {
                var deserializer   = new XmlSerializer(typeof(TR));
                var responseStream = httpResp.GetResponseStream();
                if (responseStream == null)
                {
                    throw new WebException("Blank response");
                }
                response = (TR)deserializer.Deserialize(responseStream);
            }
            httpResp.Close();
#endif
        }