public static RaiserInfo GetValueFromJson(TextReader jsonStream, string language) { RaiserInfo result = null; using (Newtonsoft.Json.JsonTextReader jtr = new Newtonsoft.Json.JsonTextReader(jsonStream)) { bool? enable = true; int? count = 0; string md5 = string.Empty, url = string.Empty, tmp; while (jtr.Read()) { if (jtr.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { if (((string)jtr.Value).ToLower() == language.ToLower()) { while (jtr.Read()) { if (jtr.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { tmp = ((string)jtr.Value).ToLower(); if (tmp == "enabled") { enable = jtr.ReadAsBoolean(); } else if (tmp == "filecount") { count = jtr.ReadAsInt32(); } else if (tmp == "patchmd5") { md5 = jtr.ReadAsString(); } else if (tmp == "patchurl") { url = jtr.ReadAsString(); } else { jtr.Skip(); } } } } else { jtr.Skip(); } } } if (enable.HasValue && !enable.Value) { result = new RaiserInfo(false, count.HasValue ? count.Value : 0, md5, url); } else { result = new RaiserInfo(true, count.HasValue ? count.Value : 0, md5, url); } } return(result); }
public void Deserialize(OperationDescription operation, Dictionary <string, int> parameterNames, Message message, object[] parameters) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException( "Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); byte[] rawBody = bodyReader.ReadContentAsBase64(); using (var ms = new MemoryStream(rawBody)) using (var sr = new StreamReader(ms)) { var serializer = new Newtonsoft.Json.JsonSerializer(); if (parameters.Length == 1) { // single parameter, assuming bare parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; reader.Read(); if (parameterNames.ContainsKey(parameterName)) { int parameterIndex = parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } sr.Close(); ms.Close(); } }
public void Deserialize(OperationDescription operation, Dictionary<string, int> parameterNames, Message message, object[] parameters) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException( "Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); byte[] rawBody = bodyReader.ReadContentAsBase64(); using (var ms = new MemoryStream(rawBody)) using (var sr = new StreamReader(ms)) { var serializer = new Newtonsoft.Json.JsonSerializer(); if (parameters.Length == 1) { // single parameter, assuming bare parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; reader.Read(); if (parameterNames.ContainsKey(parameterName)) { int parameterIndex = parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } sr.Close(); ms.Close(); } }
public void DeserializeRequest(Message message, object[] parameters) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty) == null || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); var rawBody = bodyReader.ReadContentAsBase64(); using (var ms = new MemoryStream(rawBody)) { using (var sr = new StreamReader(ms)) { var serializer = NewtonsoftJsonSettings.GetSerializer(); if (parameters.Length == 1) { if (m_operation.Messages[0].Body.Parts[0].Type == typeof(string)) { var str = sr.ReadToEnd(); var queryString = HttpUtility.ParseQueryString(str); if (queryString.AllKeys.Contains(m_operation.Messages[0].Body.Parts[0].Name)) { parameters[0] = Convert.ChangeType(queryString[m_operation.Messages[0].Body.Parts[0].Name], m_operation.Messages[0].Body.Parts[0].Type); } } else { parameters[0] = serializer.Deserialize(sr, m_operation.Messages[0].Body.Parts[0].Type); } } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; if (parameterName == null) { throw new InvalidOperationException("The object contained a parameter, however the value was null."); } reader.Read(); if (this.m_parameterNames.ContainsKey(parameterName)) { int parameterIndex = this.m_parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, this.m_operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } } } }
public void DeserializeRequest(Message message, object[] parameters) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); var rawBody = bodyReader.ReadContentAsBase64(); var ms = new MemoryStream(rawBody); var sr = new StreamReader(ms); var serializer = new Newtonsoft.Json.JsonSerializer(); if (parameters.Length == 1) { // single parameter, assuming bare parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; reader.Read(); if (parameterNames.ContainsKey(parameterName)) { var parameterIndex = parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } //Attach parameters retrieved from the Uri var templateMatchResults = (UriTemplateMatch)message.Properties["UriTemplateMatchResults"]; foreach (var parameterName in parameterNames.Where(parameterName => parameters[parameterName.Value] == null)) { if(templateMatchResults.BoundVariables.AllKeys.Contains(parameterName.Key.ToUpper())) parameters[parameterName.Value] = templateMatchResults.BoundVariables[parameterName.Key.ToUpper()]; } reader.Close(); } sr.Close(); ms.Close(); }
public void DeserializeRequest(Message message, object[] parameters) { //如果是上传的content-type,则不作json处理 var headers = ((HttpRequestMessageProperty)(message.Properties[HttpRequestMessageProperty.Name])).Headers; string contenttype = headers["Content-type"]; string contentLength = headers["Content-Length"]; if (contenttype != null && contenttype.StartsWith("multipart/form-data")) { //获得附件分割边界字符串 string boundary = contenttype.Substring(contenttype.IndexOf("boundary=") + "boundary=".Length); int len = int.Parse(contentLength); //获得方法的参数 var paramts = operation.SyncMethod.GetParameters(); int streamtypeIndx = -1; int bodyheaderlen = 0; //找到Stream类型的参数 for (streamtypeIndx = 0; streamtypeIndx < paramts.Length && streamtypeIndx < parameters.Length; streamtypeIndx++) { if (paramts[streamtypeIndx].ParameterType == typeof(Stream)) { var stream = message.GetBody <Stream>(); //定位到第一个0D0A0D0A) //sb= new StringBuilder(512); int datimes = 0;//回车换行次数 int c = 0; while (datimes != 4) { c = stream.ReadByte(); if (c == -1) { break; } if (c == 0x0d && (datimes == 0 || datimes == 2)) { datimes++; } else if (c == 0x0a && (datimes == 1 || datimes == 3)) { datimes++; } else { datimes = 0; } bodyheaderlen++; } if (c == -1) { continue; } //计算实际附件大小 int fileLength = len - bodyheaderlen - boundary.Length - 6; int remain = fileLength; MemoryStream filestream = new MemoryStream(fileLength); byte[] buffer = new byte[8192]; int readed = 0; while (remain > 0) { readed = stream.Read(buffer, 0, remain > 8192 ? 8192 : remain); remain -= readed; filestream.Write(buffer, 0, readed); } stream.Close(); filestream.Seek(0, SeekOrigin.Begin); //MemoryStream stream = new MemoryStream(message.GetReaderAtBodyContents().ReadElementContentAsBinHex()); parameters[streamtypeIndx] = filestream; } else { parameters[streamtypeIndx] = headers[paramts[streamtypeIndx].Name]; } } return; } object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents(); //begin 张辽阔 2017-01-06 添加 bodyReader.Quotas.MaxArrayLength = 163840 * 2; //end 张辽阔 2017-01-06 添加 bodyReader.ReadStartElement("Binary"); byte[] rawBody = bodyReader.ReadContentAsBase64(); MemoryStream ms = new MemoryStream(rawBody); StreamReader sr = new StreamReader(ms); Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer() { DateFormatString = "yyyy-MM-dd HH:mm:ss" }; if (parameters.Length == 1) { // single parameter, assuming bare parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { string parameterName = reader.Value as string; reader.Read(); if (this.parameterNames.ContainsKey(parameterName)) { int parameterIndex = this.parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, this.operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } sr.Close(); ms.Close(); }
public void DeserializeRequest(Message message, object[] parameters) { byte[] rawBody = null; HttpRequestMessageProperty request = null; foreach (var value in message.Properties.Values) { if (value is HttpRequestMessageProperty) { request = value as HttpRequestMessageProperty; break; } } //GET Method if (request.Method.Equals("GET", StringComparison.OrdinalIgnoreCase)) { var req = request.QueryString; var reqDecode = HttpUtility.UrlDecode(req); if (string.IsNullOrEmpty(reqDecode)) { throw new InvalidOperationException("Incoming message must have json parameter"); } rawBody = System.Text.Encoding.UTF8.GetBytes(reqDecode); } //POST Method if (request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase)) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); rawBody = bodyReader.ReadContentAsBase64(); } var ms = new MemoryStream(rawBody); var sr = new StreamReader(ms); var serializer = new Newtonsoft.Json.JsonSerializer(); if (parameters.Length == 1) { // single parameter, assuming bare if (operation.Messages[0].Body.Parts[0].Type == typeof(string)) { parameters[0] = Encoding.UTF8.GetString(rawBody); } else { parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; reader.Read(); if (this.parameterNames.ContainsKey(parameterName)) { int parameterIndex = this.parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, this.operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } sr.Close(); ms.Close(); }
public void DeserializeRequest(Message message, object[] parameters) { try { string ip = null; string via = null; string userAgent = null; object xxx; if (message.Properties.TryGetValue(RemoteEndpointMessageProperty.Name, out xxx)) { var endpoint = xxx as RemoteEndpointMessageProperty; if (endpoint != null) { ip = endpoint.Address; } } if (message.Properties.Via != null) { via = message.Properties.Via.ToString(); } if (message.Properties.TryGetValue(HttpRequestMessageProperty.Name, out xxx)) { var request = xxx as HttpRequestMessageProperty; if (request != null) { userAgent = request.Headers[HttpRequestHeader.UserAgent]; } } _applog.InfoFormat("{0}: {1} {2} {3}", ip, via, operation.Name, userAgent); if (traceSource.Switch.ShouldTrace(TraceEventType.Information)) { traceSource.TraceEvent(TraceEventType.Information, 1006, string.Format("{0}: {1} {2} {3}", ip, via, operation.Name, userAgent)); } //message.Properties.Keys.ToList().ForEach(it => _log.InfoFormat("{0}={1}", it, message.Properties[it])); //message.Headers.ToList().ForEach(it => _log.InfoFormat("{0}={1} {2} {3}", it.Name, it.Namespace,it.Actor, message.Headers.GetHeader<string>(it.Name, it.Namespace))); } catch (Exception ex) { _log.InfoFormat("{0}: error {1}", MethodBase.GetCurrentMethod().Name, ex); //traceSource.TraceEvent(TraceEventType.Error, 1000, ex); if (traceSource.Switch.ShouldTrace(TraceEventType.Warning)) { traceSource.TraceEvent(TraceEventType.Warning, 1007, ex.Message); } } object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); byte[] rawBody = bodyReader.ReadContentAsBase64(); MemoryStream ms = new MemoryStream(rawBody); StreamReader sr = new StreamReader(ms); try { Newtonsoft.Json.JsonSerializer serializer = endpoint.NewtonsoftSettings().JsonSerializer; if (parameters.Length == 1) { // single parameter, assuming bare parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { string parameterName = reader.Value as string; reader.Read(); if (this.parameterNames.ContainsKey(parameterName)) { int parameterIndex = this.parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, this.operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } if (traceSource.Switch.ShouldTrace(TraceEventType.Information)) { traceSource.TraceEvent(TraceEventType.Information, 1002, System.Text.Encoding.UTF8.GetString(rawBody)); } sr.Close(); ms.Close(); } catch { traceSource.TraceEvent(TraceEventType.Error, 1001, System.Text.Encoding.UTF8.GetString(rawBody)); throw; } }
private void recv(string msg) { if (State == WebSockState.Connecting) { if (ci.cii == null) { ci.cii = new Dictionary<string, string>(); ci.cii.Add("nick", ci.nick); ci.cii.Add("room", ci.room); } Send(make_msg("join", ci.cii)); State = WebSockState.Connected; } else if (msg == "2::") { Send("2::"); } else { msg = msg.Substring(4); Newtonsoft.Json.JsonTextReader JTR = new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(msg)); char name_type = '\0'; Dictionary<string, string> args = new Dictionary<string, string>(5); while (JTR.Read()) { switch (JTR.ValueType == typeof(string) ? (string)JTR.Value : "") { case "name": name_type = JTR.ReadAsString()[0]; break; case "args": switch (name_type) { case 'u': JTR.Read(); JTR.Read(); JTR.Read(); switch (JTR.ReadAsString()) { case "l": Dictionary<string, user_info> ui = new Dictionary<string,user_info>(8); JTR.Read(); JTR.Read(); while (JTR.Read()) { string nick = (string)JTR.Value; user_info lui = new user_info(); JTR.Read(); while (JTR.Read() && JTR.Value != null && JTR.ValueType.ToString() != "EndObject") { switch ((string)JTR.Value) { case "a": lui.admin = JTR.ReadAsInt32() == 0 ? false : true; break; case "t": lui.conn = (DateTime)JTR.ReadAsDateTime(); break; case "m": lui.mb_id = JTR.ReadAsString(); break; case "l": lui.login = JTR.ReadAsInt32() == 1 ? false : true; break; default: JTR.Read(); break; } } if(nick != null) ui.Add(nick, lui); } break; default: break; } break; case 'c': JTR.Skip(); JTR.Skip(); while (JTR.Read()) { args.Add((string)JTR.Value, JTR.ReadAsString()); } break; default: break; } break; default: break; } } } }
private void recv(string msg) { if (State == WebSockState.Connecting) { if (ci.cii == null) { ci.cii = new Dictionary <string, string>(); ci.cii.Add("nick", ci.nick); ci.cii.Add("room", ci.room); } Send(make_msg("join", ci.cii)); State = WebSockState.Connected; } else if (msg == "2::") { Send("2::"); } else { msg = msg.Substring(4); Newtonsoft.Json.JsonTextReader JTR = new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(msg)); char name_type = '\0'; Dictionary <string, string> args = new Dictionary <string, string>(5); while (JTR.Read()) { switch (JTR.ValueType == typeof(string) ? (string)JTR.Value : "") { case "name": name_type = JTR.ReadAsString()[0]; break; case "args": switch (name_type) { case 'u': JTR.Read(); JTR.Read(); JTR.Read(); switch (JTR.ReadAsString()) { case "l": Dictionary <string, user_info> ui = new Dictionary <string, user_info>(8); JTR.Read(); JTR.Read(); while (JTR.Read()) { string nick = (string)JTR.Value; user_info lui = new user_info(); JTR.Read(); while (JTR.Read() && JTR.Value != null && JTR.ValueType.ToString() != "EndObject") { switch ((string)JTR.Value) { case "a": lui.admin = JTR.ReadAsInt32() == 0 ? false : true; break; case "t": lui.conn = (DateTime)JTR.ReadAsDateTime(); break; case "m": lui.mb_id = JTR.ReadAsString(); break; case "l": lui.login = JTR.ReadAsInt32() == 1 ? false : true; break; default: JTR.Read(); break; } } if (nick != null) { ui.Add(nick, lui); } } break; default: break; } break; case 'c': JTR.Skip(); JTR.Skip(); while (JTR.Read()) { args.Add((string)JTR.Value, JTR.ReadAsString()); } break; default: break; } break; default: break; } } } }