Пример #1
0
        /// <summary>
        /// The call, with passing a binary file.
        /// </summary>
        static ApiCallResponse ApiMakeTheCall(string callurl, ParaEnums.ApiCallHttpMethod httpMethod, Attachment att)
        {
            const string boundary           = "--ParaBoundary";
            const string lineBreak          = "\r\n";
            var          contentDisposition = String.Format("Content-Disposition: {0}; name=\"{1}\"; filename=\"{1}\"", att.ContentType.MediaType, att.ContentType.Name);
            var          ac         = new ApiCallResponse();
            var          uriAddress = new Uri(callurl);

            var req = WebRequest.Create(uriAddress) as HttpWebRequest;

            req.Method        = ApiHttpPostProvider(httpMethod);
            req.KeepAlive     = false;
            ac.HttpCallMethod = req.Method;

            req.AllowWriteStreamBuffering = true;
            req.ReadWriteTimeout          = 10 * 60 * 1000;
            req.Timeout = -1;

            //Provide a way for the user to configure the connection -> proxy, timeout, etc
            ApiRequestSettings.GlobalPreRequest(req);

            req.ContentType = att.ContentType.MediaType + "; boundary:" + boundary;;

            var filebytes = new byte[Convert.ToInt32(att.ContentStream.Length - 1) + 1];

            att.ContentStream.Read(filebytes, 0, filebytes.Length);
            var sb = new StringBuilder();

            sb.AppendLine(boundary);
            sb.AppendLine(contentDisposition);
            sb.AppendLine("Content-Type: " + att.ContentType.MediaType);
            sb.AppendLine("");

            string header      = sb.ToString();
            string endboundary = lineBreak + boundary + "--";

            byte[] footerBytes = Encoding.ASCII.GetBytes(endboundary);
            byte[] headBytes   = Encoding.ASCII.GetBytes(header);

            req.ContentLength = headBytes.Length + filebytes.Length + footerBytes.Length;
            var reqStreamTest = req.GetRequestStream();
            // String to Byte Array
            var totalRequest = new byte[headBytes.Length + filebytes.Length + footerBytes.Length];

            headBytes.CopyTo(totalRequest, 0);
            filebytes.CopyTo(totalRequest, headBytes.Length);
            footerBytes.CopyTo(totalRequest, headBytes.Length + filebytes.Length);
            reqStreamTest.Write(totalRequest, 0, totalRequest.Length);

            reqStreamTest.Close();

            ac.HasException = false;
            ac.CalledUrl    = callurl;

            return(ApiHttpRequestProcessor(ac, req));
        }
Пример #2
0
        /// <summary>
        /// The true call is being made by this method.
        /// </summary>
        static ApiCallResponse ApiMakeTheCall(string callurl, ParaEnums.ApiCallHttpMethod httpMethod, XmlDocument xmlPosted)
        {
            var ac         = new ApiCallResponse();
            var uriAddress = new Uri(callurl);
            var req        = WebRequest.Create(uriAddress) as HttpWebRequest;

            req.Method        = ApiHttpPostProvider(httpMethod);
            ac.HttpCallMethod = req.Method;
            req.KeepAlive     = false;

            //2 minutes request timeout
            req.Timeout = 120 * 1000;

            if (xmlPosted != null)
            {
                req.ContentType = "application/x-www-form-urlencoded";

                // Create a byte array of the data we want to send
                var bytedata = Encoding.UTF8.GetBytes(xmlPosted.OuterXml);

                // Set the content length in the request headers
                req.ContentLength = bytedata.Length;

                //Provide a way for the user to configure the connection -> proxy, timeout, etc
                ApiRequestSettings.GlobalPreRequest(req);

                // Write data
                using (Stream postStream = req.GetRequestStream())
                {
                    postStream.Write(bytedata, 0, bytedata.Length);
                }
                ac.XmlSent = xmlPosted;
            }
            else
            {
                ac.XmlSent = null;
            }

            ac.HasException = false;
            ac.CalledUrl    = callurl;
            return(ApiHttpRequestProcessor(ac, req));
        }
Пример #3
0
        private static ApiCallResponse ApiMakeTheCall(string apiCallUrl, ParaEnums.ApiCallHttpMethod httpMethod)
        {
            var ac         = new ApiCallResponse();
            var uriAddress = new Uri(apiCallUrl);
            var req        = WebRequest.Create(uriAddress) as HttpWebRequest;

            req.Method        = ApiHttpPostProvider(httpMethod);
            ac.HttpCallMethod = req.Method;
            req.KeepAlive     = false;

            //2 minutes request timeout
            req.Timeout = 120 * 1000;

            //Provide a way for the user to configure the connection -> proxy, timeout, etc
            ApiRequestSettings.GlobalPreRequest(req);

            ac.XmlSent = null;

            ac.HasException = false;
            ac.CalledUrl    = apiCallUrl;
            return(ApiHttpRequestProcessor(ac, req));
        }
Пример #4
0
        /// <summary>
        /// Returns the Http request method for an API call, requires an ApiCallHttpMethod enum.
        /// </summary>
        internal static String ApiHttpPostProvider(ParaEnums.ApiCallHttpMethod apiCallHttpMethod)
        {
            string httpPostMethod = "";

            switch (apiCallHttpMethod)
            {
            case ParaEnums.ApiCallHttpMethod.Get:
                httpPostMethod = "GET";
                break;

            case ParaEnums.ApiCallHttpMethod.Delete:
                httpPostMethod = "DELETE";
                break;

            case ParaEnums.ApiCallHttpMethod.Post:
                httpPostMethod = "POST";
                break;

            case ParaEnums.ApiCallHttpMethod.Update:
                httpPostMethod = "PUT";
                break;
            }
            return(httpPostMethod);
        }