public Object callService(string svrname, Object reqs) { NSPLog.log(LogMsgType.NSP_LOG_NOTICE, "调用平台服务 " + svrname); //记录系统级post数据 SortedDictionary <string, string> dt = new SortedDictionary <string, string>(); if (this.isSysLevel) { dt.Add(NSP_APP, this.id); } else { dt.Add(NSP_SID, this.id); } try { if (reqs == null) { Dictionary <Object, Object> empty = new Dictionary <object, object>(); reqs = empty; } string serires = seri.Serialize(reqs); dt.Add(NSP_PARAMS, serires); dt.Add(NSP_SVC, svrname); DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); DateTime nowTime = DateTime.Now; long unixTime = (long)Math.Round((nowTime - startTime).TotalSeconds, MidpointRounding.AwayFromZero); if (ts_adjustment != 0) { dt.Add(NSP_TS, (unixTime + ts_adjustment).ToString()); } else { dt.Add(NSP_TS, unixTime.ToString()); } dt.Add(NSP_FMT, "php-rpc"); //模拟post并获取结果 string data = getPostData(this.secret, dt); NSPResponse response = request(NSP_URL, data); if (response.nsp_code == 109) { NSPLog.log(LogMsgType.NSP_LOG_ERR, "客户端系统时间有误"); ts_adjustment = (long)(DateTime.Parse(response.server_time) - nowTime).TotalSeconds; dt[NSP_TS] = (unixTime + ts_adjustment).ToString(); data = getPostData(this.secret, dt); response = request(NSP_URL, data); } return(seri.Deserialize(response.content)); } catch (Exception err) { NSPLog.log(LogMsgType.NSP_LOG_ERR, "调用服务 " + svrname + " 返回信息解析错误," + err.ToString()); throw err; } }
protected NSPResponse request(string httpurl, string data) { NSPResponse response = new NSPResponse(); NSPLog.log(LogMsgType.NSP_LOG_NOTICE, "请求url " + httpurl + "?" + data); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(httpurl + "?" + data); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.Headers.Add("Accept-Encoding", "gzip"); string strResult; HttpWebResponse res = (HttpWebResponse)req.GetResponse(); Stream smRes = res.GetResponseStream(); { strResult = res.ContentEncoding; if (strResult == "gzip") { smRes = new GZipStream(smRes, CompressionMode.Decompress); } StreamReader sr = new StreamReader(smRes, System.Text.Encoding.UTF8); strResult = sr.ReadToEnd(); sr.Close(); } if (res != null) { res.Close(); } if (smRes != null) { smRes.Close(); } response.status_code = Convert.ToInt32(res.StatusCode); response.nsp_code = Convert.ToInt32(res.Headers["NSP_STATUS"]); response.server_time = res.Headers["Date"]; response.content = strResult; NSPLog.log(LogMsgType.NSP_LOG_NOTICE, "请求url返回 " + strResult); return(response); }