VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之客户端 post ,get 访问服务器 复制代码

private void sendReuestExpansion()
    {
        HttpRequest<T> req = this;
        HttpWebRequest request;
        try
        {
            byte[] data = new byte[] { };
            if (req.PostData != null)
            {
                //HttpMethodEnum httpMethodEnum = (HttpMethodEnum)Enum.Parse(typeof(HttpMethodEnum), req.HttpMethod.ToString());
                switch (req.HttpMethod)
                {
                    case HttpMethodEnum.Get:
                        req.Url = req.Url.Contains("?") ? req.Url : (req.Url + "?");
                        foreach (var item in req.PostData.GetType().GetProperties())
                        {
                            var temp = item.GetValue(req.PostData, null);
                            if (temp != null)
                            {
                                temp = System.Web.HttpUtility.UrlEncode(Convert.ToString(temp));
                            }
                            req.Url += item.Name + "=" + temp + "&";
                        }
                        req.Url = req.Url.TrimEnd('&');
                        break;

                    case HttpMethodEnum.Post:
                        var str = (req.PostData.GetType().Name.ToLower() == "string") ? Convert.ToString(req.PostData) : JsonConvert.SerializeObject(req.PostData);
                        data = Encoding.UTF8.GetBytes(str);
                        break;

                    default:
                        break;
                }
            }
            Uri uri = new Uri(req.Url);
            request = (HttpWebRequest)WebRequest.Create(uri);
            request.Timeout = 150000;
            request.Method = req.HttpMethod.ToString().ToUpper();
            request.ContentType = "application/json; charset=utf-8";

            #region 【KeepAlive】
            /*
             HttpWebRequest的KeepAlive默认是true,如果使用的时候仅仅只是关闭流,不关闭网卡上的通道的话,第二个请求在TCP没有关闭的情况下是走同一个通道,此时本机的TCP通道就会抛异常出来,这是本机抛的错误。
             所以除了关闭本机的IO资源外,还要关闭网络资源。需要把KeepAlive设置成false就可以了。TCP通信结束后会自动关闭该请求所使用的通道。
             request.About() 是发现异常就断掉http是上层协议,底层还是走tcp的,如果不关闭的话,第二个http会默认走没有关闭的tcp。
             如果有并发的时候,数据就乱了。所以应该及时关闭tcp,每次开一个新端口。 
             */
            request.KeepAlive = false;
            #endregion

            if (data != null && data.Length > 0)
            {
                request.ContentLength = data.Length;
                Stream sm = request.GetRequestStream();
                sm.Write(data, 0, data.Length);
                sm.Flush();
                sm.Close();
            }

            StringBuilder sb = new StringBuilder("");
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse, Encoding.UTF8);
            Char[] readBuff = new Char[256];
            int count = streamRead.Read(readBuff, 0, 256);
            while (count > 0)
            {
                String outputData = new String(readBuff, 0, count);
                sb.Append(outputData);
                count = streamRead.Read(readBuff, 0, 256);
            }
            streamResponse.Close();
            streamRead.Close();

            if (response != null)
            {
                response.Close();
                response = null;
            }
            #region 【HttpWebRequest 请求释放】
            if (request != null)
            {
                request.Abort();
                request = null;
            }
            #endregion

            if (!String.IsNullOrEmpty(sb.ToString()))
            {
                resultData = HttpResult<T>.ToHttpResultByJosn(sb.ToString()); //sb.ToHttpResult<T>();
            }
            else
            { }
        }
        catch (Exception ex)
        {
            resultData = HttpResult<T>.ToHttpFaildResult(ex.Message);
        }

    }
复制代码

相关教程