private void _signatureCheck(string responseSignature, ref int statusCode, ref string responseData, ref byte[] responseByte, ref NCMBException error) { //hashデータ作成 StringBuilder stringHashData = _makeSignatureHashData(); //レスポンスデータ追加 Delete時はレスポンスデータが無いためチェックする if (responseData != null && responseData != "") { stringHashData.Append("\n" + responseData); } else if (responseByte != null) { stringHashData.Append("\n" + AsHex(responseByte)); } //シグネチャ再生成 string responseMakeSignature = _makeSignature(stringHashData.ToString()); //レスポンスシグネチャと生成したシグネチャが違う場合はエラー if (responseSignature != responseMakeSignature) { statusCode = 100; responseData = "{}"; error = new NCMBException(); error.ErrorCode = "E100001"; error.ErrorMessage = "Authentication error by response signature incorrect."; } NCMBDebug.Log("【responseSignature】 " + responseSignature); NCMBDebug.Log("【responseMakeSignature】 " + responseMakeSignature); }
/// <summary> /// リクエストの生成を行う /// </summary> internal HttpWebRequest _returnRequest() { //URLをエンコード var uri = new Uri(_url); _url = uri.AbsoluteUri; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_url); //デフォルトの生成 _makeTimeStamp(); //タイムスタンプの生成 req.Timeout = REQUEST_TIME_OUT; StringBuilder stringHashData = _makeSignatureHashData(); string result = _makeSignature(stringHashData.ToString()); //シグネチャ生成 //ヘッダー設定 //メソッド追加 switch (_method) { case ConnectType.POST: req.Method = "POST"; break; case ConnectType.PUT: req.Method = "PUT"; break; case ConnectType.GET: req.Method = "GET"; break; case ConnectType.DELETE: req.Method = "DELETE"; break; } if (req.Method.Equals("POST") && _file != null || req.Method.Equals("PUT") && _file != null) { req.ContentType = "multipart/form-data; boundary=_NCMBBoundary"; } else { req.ContentType = "application/json"; } req.Headers.Add(HEADER_APPLICATION_KEY, _applicationKey); req.Headers.Add(HEADER_SIGNATURE, result); req.Headers.Add(HEADER_TIMESTAMP_KEY, _headerTimestamp); req.Headers.Add(HEADER_USER_AGENT_KEY, HEADER_USER_AGENT_VALUE); if ((_sessionToken != null) && (_sessionToken != "")) { req.Headers.Add(HEADER_SESSION_TOKEN, _sessionToken); NCMBDebug.Log("Session token :" + _sessionToken); } req.Headers.Add(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*"); return(req); }
/// <summary> /// セッショントークン有効稼働かの処理を行う /// </summary> internal void _checkInvalidSessionToken(string code) { if (NCMBException.INCORRECT_HEADER.Equals(code)) { if ((this._sessionToken != null) && (this._sessionToken.Equals(NCMBUser._getCurrentSessionToken()))) { NCMBUser._logOutEvent(); } NCMBDebug.Log("CurrentUser is found, sessionToken info error, delete localdata"); } }
//通信処理(非同期通) 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); } }
/// <summary> /// リクエストの生成を行う /// </summary> private HttpWebRequest _returnRequest() { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_url); //デフォルトの生成 _makeTimeStamp(); //タイムスタンプの生成 req.Timeout = REQUEST_TIME_OUT; StringBuilder stringHashData = _makeSignatureHashData(); string result = _makeSignature(stringHashData.ToString()); //シグネチャ生成 //ヘッダー設定 //メソッド追加 switch (_method) { case ConnectType.POST: req.Method = "POST"; break; case ConnectType.PUT: req.Method = "PUT"; break; case ConnectType.GET: req.Method = "GET"; break; case ConnectType.DELETE: req.Method = "DELETE"; break; } req.ContentType = "application/json"; req.Headers.Add(HEADER_APPLICATION_KEY, _applicationKey); req.Headers.Add(HEADER_SIGNATURE, result); req.Headers.Add(HEADER_TIMESTAMP_KEY, _headerTimestamp); req.Headers.Add(HEADER_USER_AGENT_KEY, HEADER_USER_AGENT_VALUE); if ((_sessionToken != null) && (_sessionToken != "")) { req.Headers.Add(HEADER_SESSION_TOKEN, _sessionToken); NCMBDebug.Log("Session token :" + _sessionToken); } req.Headers.Add(HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*"); return(req); }
/// <summary> /// リクエストの生成を行う /// </summary> internal UnityWebRequest _returnRequest() { //URLをエンコード var uri = new Uri(_url); _url = uri.AbsoluteUri; //メソッド追加 String method = ""; switch (_method) { case ConnectType.POST: method = "POST"; break; case ConnectType.PUT: method = "PUT"; break; case ConnectType.GET: method = "GET"; break; case ConnectType.DELETE: method = "DELETE"; break; } UnityWebRequest req = new UnityWebRequest(_url, method); _makeTimeStamp(); //タイムスタンプの生成 StringBuilder stringHashData = _makeSignatureHashData(); string result = _makeSignature(stringHashData.ToString()); //シグネチャ生成 // ContentType設定 if (req.method.Equals("POST") && _file != null || req.method.Equals("PUT") && _file != null) { req.SetRequestHeader("Content-Type", "multipart/form-data; boundary=_NCMBBoundary"); } else { req.SetRequestHeader("Content-Type", "application/json"); } //ヘッダー設定 req.SetRequestHeader(HEADER_APPLICATION_KEY, _applicationKey); req.SetRequestHeader(HEADER_SIGNATURE, result); req.SetRequestHeader(HEADER_TIMESTAMP_KEY, _headerTimestamp); req.SetRequestHeader(HEADER_USER_AGENT_KEY, HEADER_USER_AGENT_VALUE); if ((_sessionToken != null) && (_sessionToken != "")) { req.SetRequestHeader(HEADER_SESSION_TOKEN, _sessionToken); NCMBDebug.Log("Session token :" + _sessionToken); } //req.SetRequestHeader (HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*"); if (_content != null && req.GetRequestHeader("Content-Type").Equals("multipart/form-data; boundary=_NCMBBoundary")) { _setUploadHandlerForFile(req); } else if (_content != null) { byte[] bodyRaw = Encoding.UTF8.GetBytes(_content); req.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw); } req.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); return(req); }