public ErrorTypes Request(string sUrl, string sMethod, string sContentType, byte[] aData, out byte[] aOutput)
        {
            ErrorTypes eError = ErrorTypes.NoError;
            aOutput = null;
            try
            {

                ServicePointManager.ServerCertificateValidationCallback = (s, c, h, p) => true;

                WebResponse oWebResponse = null;
                int nAttemptCount = 0;
                bool bComplete = false;
                while (!bComplete && nAttemptCount < m_nAttemptCount)
                {
                    eError = ErrorTypes.NoError;
                    try
                    {
                        HttpWebRequest oWebRequest = (HttpWebRequest)HttpWebRequest.Create(sUrl);
                        oWebRequest.UserAgent = Constants.mc_sWebClientUserAgent;
                        if (!string.IsNullOrEmpty(sMethod))
                            oWebRequest.Method = sMethod;
                        if ("POST" == oWebRequest.Method)
                        {
                            if (!string.IsNullOrEmpty(sContentType))
                                oWebRequest.ContentType = sContentType;
                            else
                                oWebRequest.ContentType = "text/plain";
                            if (null != aData)
                                oWebRequest.ContentLength = aData.Length;
                            using (Stream postStream = oWebRequest.GetRequestStream())
                            {
                                if (null != aData)
                                    postStream.Write(aData, 0, aData.Length);
                            }
                        }
                        oWebResponse = oWebRequest.GetResponse();
                        bComplete = true;
                    }
                    catch (Exception e)
                    {
                        _log.Error("Exception catched in Request:", e);
                        eError = ErrorTypes.WebRequest;
                    }

                    nAttemptCount++;
                    if (!bComplete && nAttemptCount < m_nAttemptCount)
                        Thread.Sleep((int)m_nAttemptDelay);
                }
                if (null != oWebResponse)
                {
                    Int64 ContentLength = 0;
                    if (!Int64.TryParse(oWebResponse.Headers.Get("Content-Length"), out ContentLength))
                        ContentLength = 0;
                    if (m_nMaxSize > 0 && ContentLength > m_nMaxSize)
                    {
                        _log.ErrorFormat("Downloaded object size {0} exceed max {1}", ContentLength, m_nMaxSize);
                        eError = ErrorTypes.WebRequest;
                    }
                    else
                    {
                        using (Stream oResponseStream = oWebResponse.GetResponseStream())
                        {
                            AsyncContextReadOperation oAsyncContextReadOperation = new AsyncContextReadOperation(m_nMaxSize);
                            if (ErrorTypes.NoError == oAsyncContextReadOperation.ReadContext(oResponseStream))
                                aOutput = oAsyncContextReadOperation.m_aOutput.ToArray();
                            else
                                eError = ErrorTypes.WebRequest;
                        }
                    }
                    oWebResponse.Close();
                }
            }
            catch (Exception e)
            {
                _log.Error("Exception catched in Request:", e);
                eError = ErrorTypes.WebRequest;
            }
            return eError;
        }
示例#2
0
        public ErrorTypes Request(string sUrl, string sMethod, string sContentType, byte[] aData, out byte[] aOutput)
        {
            ErrorTypes eError = ErrorTypes.NoError;

            aOutput = null;
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = (s, c, h, p) => true;

                WebResponse oWebResponse  = null;
                int         nAttemptCount = 0;
                bool        bComplete     = false;
                while (!bComplete && nAttemptCount < m_nAttemptCount)
                {
                    eError = ErrorTypes.NoError;
                    try
                    {
                        HttpWebRequest oWebRequest = (HttpWebRequest)HttpWebRequest.Create(sUrl);
                        oWebRequest.UserAgent = Constants.mc_sWebClientUserAgent;
                        if (!string.IsNullOrEmpty(sMethod))
                        {
                            oWebRequest.Method = sMethod;
                        }
                        if ("POST" == oWebRequest.Method)
                        {
                            if (!string.IsNullOrEmpty(sContentType))
                            {
                                oWebRequest.ContentType = sContentType;
                            }
                            else
                            {
                                oWebRequest.ContentType = "text/plain";
                            }
                            if (null != aData)
                            {
                                oWebRequest.ContentLength = aData.Length;
                            }
                            using (Stream postStream = oWebRequest.GetRequestStream())
                            {
                                if (null != aData)
                                {
                                    postStream.Write(aData, 0, aData.Length);
                                }
                            }
                        }
                        oWebResponse = oWebRequest.GetResponse();
                        bComplete    = true;
                    }
                    catch (Exception e)
                    {
                        _log.Error("Exception catched in Request:", e);
                        eError = ErrorTypes.WebRequest;
                    }

                    nAttemptCount++;
                    if (!bComplete && nAttemptCount < m_nAttemptCount)
                    {
                        Thread.Sleep((int)m_nAttemptDelay);
                    }
                }
                if (null != oWebResponse)
                {
                    Int64 ContentLength = 0;
                    if (!Int64.TryParse(oWebResponse.Headers.Get("Content-Length"), out ContentLength))
                    {
                        ContentLength = 0;
                    }
                    if (m_nMaxSize > 0 && ContentLength > m_nMaxSize)
                    {
                        _log.ErrorFormat("Downloaded object size {0} exceed max {1}", ContentLength, m_nMaxSize);
                        eError = ErrorTypes.WebRequest;
                    }
                    else
                    {
                        using (Stream oResponseStream = oWebResponse.GetResponseStream())
                        {
                            AsyncContextReadOperation oAsyncContextReadOperation = new AsyncContextReadOperation(m_nMaxSize);
                            if (ErrorTypes.NoError == oAsyncContextReadOperation.ReadContext(oResponseStream))
                            {
                                aOutput = oAsyncContextReadOperation.m_aOutput.ToArray();
                            }
                            else
                            {
                                eError = ErrorTypes.WebRequest;
                            }
                        }
                    }
                    oWebResponse.Close();
                }
            }
            catch (Exception e)
            {
                _log.Error("Exception catched in Request:", e);
                eError = ErrorTypes.WebRequest;
            }
            return(eError);
        }