//通信処理(同期通)
 internal void Connect(HttpClientCallback callback)
 {
     //SSLサイトにアクセスするため証明書更新
     ServicePointManager.ServerCertificateValidationCallback = delegate {
         return true;
     };
     //リクエストの作成
     HttpWebRequest req = _returnRequest ();
     //非同期データ送信
     _Connection (req, callback);
 }
 //通信処理(非同期通)
 internal void ConnectAsync(HttpClientCallback callback)
 {
     //証明書更新
     ServicePointManager.ServerCertificateValidationCallback = delegate {
         return true;
     };
     //リクエストの作成
     HttpWebRequest req = _returnRequest ();
     //非同期データ送信 BeginGetRequestStreamでくくらなければ同期通信
     if (_method == ConnectType.POST || _method == ConnectType.PUT) {
         //リクエスト非同期処理
         IAsyncResult requestResult = req.BeginGetRequestStream (ar => {
             Stream postStream = req.EndGetRequestStream (ar);
             byte[] postDataBytes = Encoding.Default.GetBytes (_content);
             postStream.Write (postDataBytes, 0, postDataBytes.Length);
             postStream.Close ();
             IAsyncResult responsResult = req.BeginGetResponse (ar2 => {
                 HttpWebResponse response = (HttpWebResponse)req.EndGetResponse (ar2);
                 Stream streamResponse = response.GetResponseStream ();
                 int statusCode = (int)response.StatusCode;
                 StreamReader streamRead = new StreamReader (streamResponse);
                 string responseData = streamRead.ReadToEnd ();
                 // リリース
                 streamResponse.Close ();
                 streamRead.Close ();
                 response.Close ();
                 callback (statusCode, responseData, null);
             }, null);
         }, null);
     } else if (_method == ConnectType.GET || _method == ConnectType.DELETE) {
         IAsyncResult responseResult = req.BeginGetResponse (ar => {
             try {
                 HttpWebResponse res = (HttpWebResponse)req.EndGetResponse (ar);
                 int statusCode = (int)res.StatusCode;
                 Stream streamResponse = res.GetResponseStream ();
                 StreamReader streamRead = new StreamReader (streamResponse);
                 string responseData = streamRead.ReadToEnd ();
                 callback (statusCode, responseData, null);
             } catch (WebException e) {
                 NCMBDebug.LogError ("失敗error:" + e);
             }
         }, null);
     }
 }
        private void _Connection(HttpWebRequest req, HttpClientCallback callback)
        {
            int statusCode = 0;
            string responseData = null;
            NCMBException error = null;

            //Post,Put時のコンテントデータ書き込み
            if (_method == ConnectType.POST || _method == ConnectType.PUT) {
                req = this._sendRequest (req, ref error);
                //書き込みでエラーがあれば終了
                if (error != null) {
                    callback (statusCode, responseData, error);
                    return;
                }
            }

            HttpWebResponse httpResponse = null;
            Stream streamResponse = null;
            StreamReader streamRead = null;

            try {
                //通常処理
                httpResponse = (HttpWebResponse)req.GetResponse ();
                streamResponse = httpResponse.GetResponseStream ();
                statusCode = (int)httpResponse.StatusCode;
                streamRead = new StreamReader (streamResponse);
                responseData = streamRead.ReadToEnd ();

            } catch (WebException ex) {

                //API側からのエラー処理
                using (WebResponse webResponse = ex.Response) {
                    error = new NCMBException ();
                    error.ErrorMessage = ex.Message;

                    streamResponse = webResponse.GetResponseStream ();
                    streamRead = new StreamReader (streamResponse);
                    responseData = streamRead.ReadToEnd ();

                    var jsonData = MiniJSON.Json.Deserialize (responseData) as Dictionary<string,object>;
                    var hashtableData = new Hashtable (jsonData);

                    error.ErrorCode = (hashtableData ["code"].ToString ());
                    error.ErrorMessage = (hashtableData ["error"].ToString ());

                    httpResponse = (HttpWebResponse)webResponse;
                    statusCode = (int)httpResponse.StatusCode;
                }
            } finally {
                if (httpResponse != null) {
                    httpResponse.Close ();
                }
                if (streamResponse != null) {
                    streamResponse.Close ();
                }
                if (streamRead != null) {
                    streamRead.Close ();
                }
                //check if session token error have or not
                if (error != null) {
                    _checkInvalidSessionToken (error.ErrorCode);
                }

                //レスポンスシグネチャのチェック Flagがtureかつエラーコードが以下の三つ以外の時はチェックを行う
                //if (NCMBSettings._responseValidationFlag && (errorCode != "E404002" && errorCode != "E405001" && errorCode != "E415001")) {
                if (NCMBSettings._responseValidationFlag && httpResponse != null) {

                    //レスポンスシグネチャが無い場合はE100001エラー
                    if (httpResponse.Headers.GetValues (RESPONSE_SIGNATURE) != null) {
                        string responseSignature = httpResponse.Headers.GetValues (RESPONSE_SIGNATURE) [0];
                        _signatureCheck (responseSignature, ref statusCode, ref responseData, ref error);
                    } else {
                        statusCode = 100;
                        responseData = "{}";
                        error = new NCMBException ();
                        error.ErrorCode = "E100001";
                        error.ErrorMessage = "Authentication error by response signature incorrect.";
                    }
                }

                callback (statusCode, responseData, error);
            }
        }
        private void _Connection(HttpWebRequest req, HttpClientCallback callback)
        {
            int statusCode = 0;
            string responseData = null;
            NCMBException error = null;

            //Post,Put時のコンテントデータ書き込み
            if (_method == ConnectType.POST || _method == ConnectType.PUT) {
                req = this._sendRequest (req, ref error);
                //書き込みでエラーがあれば終了
                if (error != null) {
                    callback (statusCode, responseData, error);
                    return;
                }
            }

            HttpWebResponse httpResponse = null;
            Stream streamResponse = null;
            StreamReader streamRead = null;

            try {
                //通常処理
                httpResponse = (HttpWebResponse)req.GetResponse ();//①通信
                streamResponse = httpResponse.GetResponseStream (); //②通信結果からResponseデータ作成
                statusCode = (int)httpResponse.StatusCode; //③Responseデータからステータスコード取得
                streamRead = new StreamReader (streamResponse); //④Responseデータからデータ取得
                responseData = streamRead.ReadToEnd ();//⑤書き出したデータを全てstringに書き出し

            } catch (WebException ex) {

                //API側からのエラー処理
                using (WebResponse webResponse = ex.Response) {//①WebExceptionからWebResponseを取得
                    error = new NCMBException ();
                    error.ErrorMessage = ex.Message;

                    streamResponse = webResponse.GetResponseStream ();//②WebResponsからResponseデータ作成
                    streamRead = new StreamReader (streamResponse); //③Responseデータからデータ取得
                    responseData = streamRead.ReadToEnd ();//④書き出したデータを全てstringに書き出し

                    var jsonData = MiniJSON.Json.Deserialize (responseData) as Dictionary<string,object>;//⑤Dictionaryに変換
                    var hashtableData = new Hashtable (jsonData);//⑥Hashtableに変換 必要?

                    error.ErrorCode = (hashtableData ["code"].ToString ());//⑦Hashtableから各keyのvalue取得
                    error.ErrorMessage = (hashtableData ["error"].ToString ());

                    httpResponse = (HttpWebResponse)webResponse;//⑧WebResponseをHttpWebResponseに変換
                    statusCode = (int)httpResponse.StatusCode;//⑨httpWebResponseからステータスコード取得
                }
            } finally {
                if (httpResponse != null) {
                    httpResponse.Close ();
                }
                if (streamResponse != null) {
                    streamResponse.Close ();
                }
                if (streamRead != null) {
                    streamRead.Close ();
                }
                //check if session token error have or not
                if (error != null) {
                    _checkInvalidSessionToken (error.ErrorCode);
                }

                //レスポンスデータにエスケープシーケンスがあればアンエスケープし、mobile backend上と同一にします
                if (responseData != null) {
                    if(responseData != Regex.Unescape(responseData)){
                        UnityEngine.Debug.Log ("Unescape start."+responseData);
                        responseData = Regex.Unescape (responseData);
                        UnityEngine.Debug.Log ("Unescape finish."+responseData);
                    }
                }

                //レスポンスシグネチャのチェック Flagがtureかつエラーコードが以下の三つ以外の時はチェックを行う
                //if (NCMBSettings._responseValidationFlag && (errorCode != "E404002" && errorCode != "E405001" && errorCode != "E415001")) {
                if (NCMBSettings._responseValidationFlag && httpResponse != null) {

                    //レスポンスシグネチャが無い場合はE100001エラー
                    if (httpResponse.Headers.GetValues (RESPONSE_SIGNATURE) != null) {
                        string responseSignature = httpResponse.Headers.GetValues (RESPONSE_SIGNATURE) [0];
                        _signatureCheck (responseSignature, ref statusCode, ref responseData, ref error);
                    } else {
                        statusCode = 100;
                        responseData = "{}";
                        error = new NCMBException ();
                        error.ErrorCode = "E100001";
                        error.ErrorMessage = "Authentication error by response signature incorrect.";
                    }
                }

                callback (statusCode, responseData, error);
            }
        }
 //通信処理(同期通)
 internal void Connect(HttpClientCallback callback)
 {
     //try {
     //証明書更新 更新しないとSSLサイトにアクセス出来ない
     ServicePointManager.ServerCertificateValidationCallback = delegate {
         return true;
     };
     //リクエストの作成
     HttpWebRequest req = _returnRequest ();
     //非同期データ送信 BeginGetRequestStreamでくくらなければ同期通信
     _Connection (req, callback);
 }
示例#6
0
        private void _Connection(HttpWebRequest req, HttpClientCallback callback)
        {
            int statusCode = 0;
            string responseData = null;
            NCMBException error = null;

            //Post,Put時のコンテントデータ書き込み
            if (_method == ConnectType.POST || _method == ConnectType.PUT) {
                req = this._sendRequest (req, ref error);
                //書き込みでエラーがあれば終了
                if (error != null) {
                    callback (statusCode, responseData, error);
                    return;
                }
            }

            HttpWebResponse httpResponse = null;
            Stream streamResponse = null;
            StreamReader streamRead = null;

            try {
                //通常処理
                httpResponse = (HttpWebResponse)req.GetResponse ();//通信
                streamResponse = httpResponse.GetResponseStream (); //通信結果からResponseデータ作成
                statusCode = (int)httpResponse.StatusCode; //Responseデータからステータスコード取得
                streamRead = new StreamReader (streamResponse); //Responseデータからデータ取得
                responseData = streamRead.ReadToEnd ();//書き出したデータを全てstringに書き出し

            } catch (WebException ex) {

                //API側からのエラー処理
                using (WebResponse webResponse = ex.Response) {//WebExceptionからWebResponseを取得

                    error = new NCMBException ();
                    if (webResponse != null) {
                        streamResponse = webResponse.GetResponseStream ();//WebResponsからResponseデータ作成
                        streamRead = new StreamReader (streamResponse); //Responseデータからデータ取得
                        responseData = streamRead.ReadToEnd ();//書き出したデータを全てstringに書き出し
                        var jsonData = MiniJSON.Json.Deserialize (responseData) as Dictionary<string,object>;//Dictionaryに変換
                        var hashtableData = new Hashtable (jsonData);//Hashtableに変換

                        error.ErrorCode = (hashtableData ["code"].ToString ());//Hashtableから各keyのvalue取得
                        error.ErrorMessage = (hashtableData ["error"].ToString ());

                        httpResponse = (HttpWebResponse)webResponse;//WebResponseをHttpWebResponseに変換
                        statusCode = (int)httpResponse.StatusCode;//httpWebResponseからステータスコード取得
                    } else {
                        error.ErrorMessage = ex.Message;
                        error.ErrorCode = ((int)ex.Status).ToString();
                    }

                }
            } finally {
                if (httpResponse != null) {
                    httpResponse.Close ();
                }
                if (streamResponse != null) {
                    streamResponse.Close ();
                }
                if (streamRead != null) {
                    streamRead.Close ();
                }
                //check if session token error have or not
                if (error != null) {
                    _checkInvalidSessionToken (error.ErrorCode);
                }

                //レスポンスシグネチャのチェック
                if (NCMBSettings._responseValidationFlag && httpResponse != null) {

                    //レスポンスシグネチャが無い場合はE100001エラー
                    if (httpResponse.Headers.GetValues (RESPONSE_SIGNATURE) != null) {
                        string responseSignature = httpResponse.Headers.GetValues (RESPONSE_SIGNATURE) [0];
                        _signatureCheck (responseSignature, ref statusCode, ref responseData, ref error);
                    } else {
                        statusCode = 100;
                        responseData = "{}";
                        error = new NCMBException ();
                        error.ErrorCode = "E100001";
                        error.ErrorMessage = "Authentication error by response signature incorrect.";
                    }
                }

                callback (statusCode, responseData, error);
            }
        }