public static MessageReader Create(string serverUrl, string requestParams, ref Message header, bool IsSocket) { MessageReader msgReader = null; if (IsSocket) { SocketAction socketAction = new SocketAction(); socketAction.DoSocket(serverUrl, requestParams); msgReader = socketAction.result; header = socketAction._head; //MemoryStream ms = new MemoryStream(socketAction.result.ReadByte()); //BinaryReader reader = new BinaryReader(ms, Encoding.UTF8); //msgReader = new MessageReader(reader); } else { Encoding encode = Encoding.GetEncoding("utf-8"); string postData = "d=" + GetSign(requestParams); byte[] bufferData = encode.GetBytes(postData); HttpWebRequest serverRequest = (HttpWebRequest)WebRequest.Create(serverUrl); serverRequest.Method = "POST"; serverRequest.ContentType = "application/x-www-form-urlencoded"; serverRequest.ContentLength = bufferData.Length; Stream requestStream = serverRequest.GetRequestStream(); requestStream.Write(bufferData, 0, bufferData.Length); requestStream.Close(); //返回流 WebResponse serverResponse = serverRequest.GetResponse(); Stream responseStream = serverResponse.GetResponseStream(); if (responseStream != null) { BinaryReader reader = new BinaryReader(responseStream, Encoding.UTF8); msgReader = new MessageReader(reader); header.Length = msgReader.ReadInt(); header.ErrorCode = msgReader.ReadInt(); header.MsgID = msgReader.ReadInt(); header.ErrorInfo = msgReader.ReadString(); header.Action = msgReader.ReadInt(); header.St = msgReader.ReadString(); } } return(msgReader); }
public static MessageReader Create(string serverUrl, string requestParams, Message header, bool IsSocket) { MessageReader msgReader = null; if (IsSocket) { SocketAction socketAction = new SocketAction(); socketAction.DoSocket(serverUrl, requestParams); MemoryStream ms = new MemoryStream(socketAction.result.ReadBuffer()); BinaryReader reader = new BinaryReader(ms, Encoding.UTF8); msgReader = new MessageReader(reader); } else { Encoding encode = Encoding.GetEncoding("utf-8"); string postData = "d=" + GetSign(requestParams); byte[] bufferData = encode.GetBytes(postData); HttpWebRequest serverRequest = (HttpWebRequest)WebRequest.Create(serverUrl); serverRequest.Method = "POST"; serverRequest.ContentType = "application/x-www-form-urlencoded"; serverRequest.ContentLength = bufferData.Length; Stream requestStream = serverRequest.GetRequestStream(); requestStream.Write(bufferData, 0, bufferData.Length); requestStream.Close(); //返回流 WebResponse serverResponse = serverRequest.GetResponse(); Stream responseStream = serverResponse.GetResponseStream(); if (responseStream != null) { BinaryReader reader = new BinaryReader(responseStream, Encoding.UTF8); msgReader = new MessageReader(reader); header.Length = msgReader.ReadInt(); header.ErrorCode = msgReader.ReadInt(); header.MsgID = msgReader.ReadInt(); header.ErrorInfo = msgReader.ReadString(); header.Action = msgReader.ReadInt(); header.St = msgReader.ReadString(); } } return msgReader; }