Exemplo n.º 1
0
        public IStreamImplementation GetMsgStreamImpl(JdCometStreamRequest cometReq)
        {
            if (cometReq != null)
            {
                cometReq.GetConnectListener().OnBeforeConnect();
            }

            JdDictionary param = new JdDictionary();

            param.Add(StreamConstants.PARAM_APPKEY, cometReq.GetAppkey());
            if (!String.IsNullOrEmpty(cometReq.GetUserId()))
            {
                param.Add(StreamConstants.PARAM_USERID, cometReq.GetUserId());
            }
            if (!String.IsNullOrEmpty(cometReq.GetConnectId()))
            {
                param.Add(StreamConstants.PARAM_CONNECT_ID, cometReq.GetConnectId());
            }
            param.Add(StreamConstants.PARAM_TIMESTAMP, DateTime.Now.Ticks);

            IDictionary <string, string> otherParam = cometReq.GetOtherParam();

            if (otherParam != null && otherParam.Count > 0)
            {
                IEnumerator <KeyValuePair <string, string> > kvps = otherParam.GetEnumerator();
                while (kvps.MoveNext())
                {
                    param.Add(kvps.Current.Key, kvps.Current.Value);
                }
            }

            string sign = null;

            try
            {
                sign = JdUtils.SignJdRequest(param, cometReq.GetSecret(), true);
                if (String.IsNullOrEmpty(sign))
                {
                    throw new Exception("Get sign error");
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            param.Add(StreamConstants.PARAM_SIGN, sign);
            HttpClient   httpClient = new HttpClient(conf, param);
            HttpResponse response;

            response = httpClient.Post();
            return(currentStreamImpl = new MessageStreamImpl(msgConsumeFactory, response, cometMessageListener, this));
        }
Exemplo n.º 2
0
        private void doConnect(string uri)
        {
            var signHeader = new Dictionary <string, string>();
            var connHeader = new Dictionary <string, object>();

            signHeader.Add(APP_KEY, this._id.AppKey);
            connHeader.Add(APP_KEY, signHeader[APP_KEY]);
            signHeader.Add(GROUP_NAME, this._id.GroupName);
            connHeader.Add(GROUP_NAME, signHeader[GROUP_NAME]);
            signHeader.Add(TIMESTAMP, DateTime.Now.Ticks.ToString());
            connHeader.Add(TIMESTAMP, signHeader[TIMESTAMP]);
            connHeader.Add(SIGN, JdUtils.SignJdRequest(signHeader, this._appSecret, true));
            //extra fields
            connHeader.Add(SDK, DefaultJdClient.SDK_VERSION);
            this._serverProxy = this._endpoint.GetEndpoint(new TmcServerIdentity(), uri, connHeader);
            this._uri         = uri;
            this.Log.InfoFormat("connected to tmc server: {0}", uri);
        }
Exemplo n.º 3
0
        private T DoExecute <T> (IJdRequest <T> request, string access_token, DateTime timestamp) where T : JdResponse
        {
            // 提前检查业务参数
            try {
                request.Validate();
            } catch (JdException e) {
                return(createErrorResponse <T> (e.ErrorCode, e.ErrorMsg));
            }

            // 添加协议级请求参数
            JdDictionary txtParams = new JdDictionary();

            txtParams.Add(METHOD, request.GetApiName());
            txtParams.Add(VERSION, "2.0");
            txtParams.Add(APP_KEY, appKey);
            txtParams.Add(TIMESTAMP, timestamp);
            if (access_token != null)
            {
                txtParams.Add(ACCESS_TOKEN, access_token);
            }
            txtParams.Add(PARAM_JSON, JsonConvert.ExportToString(request.GetParameters()));
            // 添加签名参数
            txtParams.Add(SIGN, JdUtils.SignJdRequest(txtParams, appSecret, true));
            string reqUrl = webUtils.BuildGetUrl(this.serverUrl, txtParams);

            try {
                string body;
                if (request is IJdUploadRequest <T> )                 // 是否需要上传文件
                {
                    IJdUploadRequest <T>           uRequest   = (IJdUploadRequest <T>)request;
                    IDictionary <string, FileItem> fileParams = JdUtils.CleanupDictionary(uRequest.GetFileParameters());
                    body = webUtils.DoPost(this.serverUrl, txtParams, fileParams);
                }
                else
                {
                    body = webUtils.DoPost(this.serverUrl, txtParams);
                }

                // 解释响应结果
                T rsp;
                if (disableParser)
                {
                    rsp      = Activator.CreateInstance <T> ();
                    rsp.Body = body;
                }
                else
                {
                    if (FORMAT_XML.Equals(format))
                    {
                        IJdParser tp = new JdXmlParser();
                        rsp = tp.Parse <T> (body);
                    }
                    else
                    {
                        IJdParser tp = new JdJsonParser();
                        rsp = tp.Parse <T> (body);
                    }
                }

                // 追踪错误的请求
                if (!disableTrace && rsp.IsError)
                {
                    StringBuilder sb = new StringBuilder(reqUrl).Append(" response error!\r\n").Append(rsp.Body);
                    JdLogger.Warn(sb.ToString());
                }
                return(rsp);
            } catch (Exception e) {
                if (!disableTrace)
                {
                    StringBuilder sb = new StringBuilder(reqUrl).Append(" request error!\r\n").Append(e.StackTrace);
                    JdLogger.Error(sb.ToString());
                }
                throw e;
            }
        }