Пример #1
0
        public static MessageStructure Create(string serverUrl, string requestParams, out MessageHead header, bool isSocket, int actionId, string pid)
        {
            header = null;
            MessageStructure msgReader = null;

            if (isSocket)
            {
                msgReader = DoRequest(serverUrl, requestParams, actionId, pid);
            }
            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();
                msgReader = MessageStructure.Create(responseStream, Encoding.UTF8);
            }
            if (msgReader != null)
            {
                header = msgReader.ReadHeadGzip();
            }
            return(msgReader);
        }
Пример #2
0
        private static string PostGameServer(List <ParamInfoModel> paramList, int contractId, string serverUrl, string requestParams, string pid, bool includeParam, int contentType, int showType, CookieContainer cookies, out string sid, out string uid, out string st)
        {
            bool isSocket = !serverUrl.StartsWith("http://");

            sid = "";
            uid = "";
            st  = "";
            StringBuilder respContent    = new StringBuilder();
            MessageHead   header         = null;
            var           responseStream = NetHelper.Create(serverUrl, requestParams, isSocket, contractId, pid, includeParam, cookies);

            switch (contentType)
            {
            case 0:     //stream
                var msgReader = MessageStructure.Create(responseStream, Encoding.UTF8);
                if (msgReader != null)
                {
                    header = msgReader.ReadHeadGzip();
                    if (showType == 1)     //table
                    {
                        ProcessResult(paramList, contractId, respContent, header, msgReader, out sid, out uid, out st);
                    }
                    else if (showType == 2)     //json
                    {
                        respContent.AppendLine("未实现此格式显示");
                    }
                }
                else
                {
                    ResponseHead(contractId, respContent, ErrorCode, "请求超时");
                }
                break;

            case 1:     //json
                using (var sr = new System.IO.StreamReader(responseStream))
                {
                    respContent.AppendLine(sr.ReadToEnd());
                }
                break;

            default:
                throw new ArgumentOutOfRangeException("contentType");
            }
            return(respContent.ToString());
        }