Exemplo n.º 1
0
        /// <summary>
        /// 发送POST数据
        /// </summary>
        /// <param name="message">消息</param>
        /// <returns>返回消息</returns>
        public int SendRequest(CMessage message)
        {
            Binary bw = new Binary();

            byte[] body          = message.m_body;
            int    bodyLength    = message.m_bodyLength;
            int    uncBodyLength = bodyLength;

            if (message.m_compressType == COMPRESSTYPE_GZIP)
            {
                using (MemoryStream cms = new MemoryStream())
                {
                    using (GZipStream gzip = new GZipStream(cms, CompressionMode.Compress))
                    {
                        gzip.Write(body, 0, body.Length);
                    }
                    body       = cms.ToArray();
                    bodyLength = body.Length;
                }
            }
            int len = sizeof(int) * 4 + bodyLength + sizeof(short) * 3 + sizeof(byte) * 2;

            bw.WriteInt(len);
            bw.WriteShort((short)message.m_groupID);
            bw.WriteShort((short)message.m_serviceID);
            bw.WriteShort((short)message.m_functionID);
            bw.WriteInt(message.m_sessionID);
            bw.WriteInt(message.m_requestID);
            bw.WriteByte((byte)message.m_state);
            bw.WriteByte((byte)message.m_compressType);
            bw.WriteInt(uncBodyLength);
            bw.WriteBytes(body);
            byte[]         bytes  = bw.GetBytes();
            int            length = bytes.Length;
            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(m_url);

            webReq.Method        = "POST";
            webReq.ContentType   = "application/x-www-form-urlencoded";
            webReq.ContentLength = bytes.Length;
            if (bytes != null)
            {
                Stream writer = webReq.GetRequestStream();
                writer.Write(bytes, 0, bytes.Length);
                writer.Close();
            }
            HttpWebResponse response      = (HttpWebResponse)webReq.GetResponse();
            Stream          reader        = response.GetResponseStream();
            long            contentLength = response.ContentLength;

            byte[] dataArray = new byte[contentLength];
            for (int i = 0; i < contentLength; i++)
            {
                dataArray[i] = (byte)reader.ReadByte();
            }
            response.Close();
            reader.Dispose();
            bw.Close();
            int ret = dataArray.Length;

            UpFlow += ret;
            IntPtr ptr = Marshal.AllocHGlobal(sizeof(byte) * ret);

            for (int i = 0; i < ret; i++)
            {
                IntPtr iptr = (IntPtr)((int)ptr + i);
                Marshal.WriteByte(iptr, dataArray[i]);
            }
            BaseService.CallBack(message.m_socketID, 0, ptr, ret);
            Marshal.FreeHGlobal(ptr);
            return(ret);
        }