Exemplo n.º 1
0
        public void HttpAsyncEvent(Msg msg)
        {
            if (msg == null)
            {
                return;
            }
            byte[]         data      = encoder.encode(msg);
            HttpWebRequest myRequest = null;
            Stream         stream    = null;

            try
            {
                // 发送请求
                myRequest                 = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Timeout         = 30000;
                myRequest.KeepAlive       = false;
                myRequest.CookieContainer = myCookie;
                myRequest.Method          = "POST";
                myRequest.ContentLength   = data.Length;
                stream = myRequest.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Flush();
                // 获得回复
                HttpWebResponse myResponse     = (HttpWebResponse)myRequest.GetResponse();
                Stream          responseStream = myResponse.GetResponseStream();
                HttpStatusCode  code           = myResponse.StatusCode;
                if (code == HttpStatusCode.OK)
                {
                    byte[]   temp   = new byte[128];
                    int      i      = 0;
                    IoBuffer buffer = new IoBuffer();
                    do
                    {
                        i = responseStream.Read(temp, 0, 128);
                        if (i > 0)
                        {
                            buffer.Write(temp, 0, i);
                        }
                    } while (i > 0);
                    responseStream.Close();
                    int    len   = buffer.Length;
                    byte[] datas = new byte[len];
                    buffer.Read(datas, 0, len);

                    //单包模型
                    Msg revMsg = decoder.DeCode(datas);
                    if (logReport != null)
                    {
                        logReport.OnLogReport("rev http rsp,cmd:" + revMsg.Cmd + " rsCode:" + revMsg.GetParam(BaseCodeMap.BaseParam.RS_CODE));
                    }
                    mDataPackets.Put(new RevEvent(this, revMsg));
                }
                else
                {
                    if (logReport != null)
                    {
                        logReport.OnWarningReport("http cmd:" + msg.Cmd + " fail,code:" + code);
                    }
                    msg.AddParam(BaseCodeMap.BaseParam.RS_CODE, BaseCodeMap.BaseRsCode.TIME_OUT);
                    mDataPackets.Put(new TimeOutEvent(this, msg));
                }

                return;
            }
            catch (Exception ex)
            {
                if (logReport != null)
                {
                    logReport.OnWarningReport("http cmd:" + msg.Cmd + " ex:" + ex.ToString());
                }
                mDataPackets.Put(new TimeOutEvent(this, msg));
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }