예제 #1
0
        public override IHTTPResponse performSync()
        {
            OnStart();
            WWW          wWW          = new WWW(getUrl());
            ServicePoint servicePoint = ServicePointManager.FindServicePoint(getUrl(), null);

            if (servicePoint != null)
            {
                Debug.Log("Located service point.");
                X509Certificate certificate = servicePoint.Certificate;
                if (certificate != null)
                {
                    Debug.Log("Certificate hash: " + certificate.GetCertHash());
                }
            }
            else
            {
                Debug.Log("None found");
            }
            HTTPBaseResponseImpl hTTPBaseResponseImpl = new HTTPBaseResponseImpl(this);

            if (!string.IsNullOrEmpty(wWW.error))
            {
                hTTPBaseResponseImpl.setStatusCode(parseStatusFromMessage(wWW.error));
                OnError(hTTPBaseResponseImpl, new HTTPException(wWW.error));
            }
            else
            {
                hTTPBaseResponseImpl.setStatusCode(200);
                hTTPBaseResponseImpl.setDocument(new HTTPBaseDocumentImpl(wWW.bytes));
                OnSuccess(hTTPBaseResponseImpl);
            }
            OnComplete();
            return(hTTPBaseResponseImpl);
        }
예제 #2
0
        private IEnumerator request()
        {
            OnStart();
            WWW          www = new WWW(getUrl());
            ServicePoint sp  = ServicePointManager.FindServicePoint(getUrl(), null);

            if (sp != null)
            {
                Debug.Log("Located service point.");
                X509Certificate certificate = sp.Certificate;
                if (certificate != null)
                {
                    Debug.Log("Certificate hash: " + certificate.GetCertHash());
                }
            }
            else
            {
                Debug.Log("None found");
            }
            yield return(www);

            if (!www.isDone)
            {
                yield return(www);
            }
            HTTPBaseResponseImpl response = new HTTPBaseResponseImpl(this);

            if (!string.IsNullOrEmpty(www.error))
            {
                response.setStatusCode(parseStatusFromMessage(www.error));
                OnError(response, new HTTPException(www.error));
            }
            else
            {
                response.setStatusCode(200);
                response.setDocument(new HTTPBaseDocumentImpl(www.bytes));
                OnSuccess(response);
            }
            OnComplete();
        }
예제 #3
0
        private IHTTPResponse send()
        {
            byte[] array = new byte[DOWNLOAD_BUFFER_SIZE];
            ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidationCallback;
            Stopwatch      stopwatch      = new Stopwatch();
            HttpWebRequest httpWebRequest = null;

            byte[] array2 = null;
            int    num    = 0;
            HTTPBaseResponseImpl hTTPBaseResponseImpl = new HTTPBaseResponseImpl(this);

            try
            {
                httpWebRequest        = (HttpWebRequest)WebRequest.Create(getUrl());
                httpWebRequest.Method = getMethod().ToString();
                ServicePointManager.Expect100Continue = false;
                foreach (string key in getRequestHeaders().Keys)
                {
                    foreach (string item in getRequestHeaders()[key])
                    {
                        try
                        {
                            if (key.Equals("Content-Type"))
                            {
                                httpWebRequest.ContentType = item;
                            }
                            else if (key.Equals("Content-Length"))
                            {
                                httpWebRequest.ContentLength = Convert.ToInt64(item);
                            }
                            else if (key.Equals("Accept"))
                            {
                                httpWebRequest.Accept = item;
                            }
                            else
                            {
                                httpWebRequest.Headers.Set(key, item);
                            }
                        }
                        catch (ArgumentException ex)
                        {
                            UnityEngine.Debug.LogWarning("Failed to add the header named '" + key + "' to the request. Exception: " + ex.Message);
                        }
                    }
                }
                if ((getMethod() == HTTPMethod.POST || getMethod() == HTTPMethod.PUT) && getDocument() != null)
                {
                    array2 = getDocument().getData();
                    if (array2 != null && array2.Length > 0)
                    {
                        httpWebRequest.ContentLength = array2.Length;
                        using (Stream stream = httpWebRequest.GetRequestStream())
                        {
                            stream.Write(array2, 0, array2.Length);
                            stream.Close();
                        }
                    }
                }
                stopwatch.Start();
                HttpWebResponse httpWebResponse = null;
                Exception       ex2             = null;
                try
                {
                    httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                }
                catch (WebException ex3)
                {
                    UnityEngine.Debug.LogException(ex3);
                    ex2 = ex3;
                    if (ex3.Status == WebExceptionStatus.ProtocolError)
                    {
                        httpWebResponse = (HttpWebResponse)ex3.Response;
                    }
                }
                catch (SocketException ex4)
                {
                    UnityEngine.Debug.LogException(ex4);
                    ex2 = ex4;
                }
                long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
                if (httpWebResponse == null)
                {
                    if (ThreadedHTTPFactory.LogAllRequests)
                    {
                        UnityEngine.Debug.Log(InfoString(httpWebRequest, array2, null, null, elapsedMilliseconds, ThreadedHTTPFactory.VerboseLogging));
                    }
                }
                else
                {
                    byte[] array3 = null;
                    using (Stream stream2 = httpWebResponse.GetResponseStream())
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            int num2 = 0;
                            while ((num2 = stream2.Read(array, 0, array.Length)) > 0 && (ThreadBase.CurrentThread == null || !ThreadBase.CurrentThread.ShouldStop))
                            {
                                memoryStream.Write(array, 0, num2);
                                num += num2;
                                OnProgress(array, num2, num, -1);
                            }
                            array3 = memoryStream.ToArray();
                            memoryStream.Close();
                        }
                        stream2.Close();
                    }
                    if (ThreadedHTTPFactory.LogAllRequests)
                    {
                        UnityEngine.Debug.Log(InfoString(httpWebRequest, array2, httpWebResponse, array3, elapsedMilliseconds, ThreadedHTTPFactory.VerboseLogging));
                    }
                    hTTPBaseResponseImpl.setStatusCode((int)httpWebResponse.StatusCode);
                    hTTPBaseResponseImpl.setReasonPhrase(httpWebResponse.StatusDescription);
                    HTTPBaseDocumentImpl hTTPBaseDocumentImpl = new HTTPBaseDocumentImpl(array3);
                    hTTPBaseResponseImpl.setDocument(hTTPBaseDocumentImpl);
                }
                if (ex2 == null)
                {
                    OnSuccess(hTTPBaseResponseImpl);
                }
                else
                {
                    OnError(hTTPBaseResponseImpl, ex2);
                }
            }
            catch (Exception exception)
            {
                UnityEngine.Debug.LogException(exception);
                OnError(hTTPBaseResponseImpl, exception);
            }
            OnComplete();
            return(hTTPBaseResponseImpl);
        }