首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET Framework >

HttpWebRequest发送post请求有关问题

2014-01-17 
HttpWebRequest发送post请求问题HttpWebRequest request WebRequest.Create(uri) as HttpWebRequest//

HttpWebRequest发送post请求问题
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
                //请求方式
                request.Method = "POST";
                //超时时间
                //request.Timeout = 300000;
                request.ContentType = "text/xml";
                byte[] payload = Encoding.UTF8.GetBytes(message);
                request.ContentLength = payload.Length;
                //获得请求流
                writer = request.GetRequestStream();
                ////将请求参数写入流
                writer.Write(payload, 0, payload.Length);

                //关闭请求流
                writer.Flush();
                writer.Close();

                reader = new StreamReader(response.GetResponseStream());
                string responseData = reader.ReadToEnd();
                response.Close();
                reader.Close();

代码如上所示,当我循环执行多次这个代码时,导致连接无法释放
查看端口效果如下:
HttpWebRequest发送post请求有关问题
求大神指教啊,急啊,当什么都不发的时候,循环多次没有任何问题
[解决办法]
response 那里冒出来的?你的代码完整
[解决办法]

引用:
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
                //请求方式
                request.Method = "POST";
                //超时时间
                //request.Timeout = 300000;
                request.ContentType = "text/xml";
                byte[] payload = Encoding.UTF8.GetBytes(message);
                request.ContentLength = payload.Length;
                //获得请求流
                writer = request.GetRequestStream();
                ////将请求参数写入流
                writer.Write(payload, 0, payload.Length);

                //关闭请求流
                writer.Flush();
                writer.Close();

                reader = new StreamReader(response.GetResponseStream());
                string responseData = reader.ReadToEnd();
                response.Close();


                reader.Close();

代码如上所示,当我循环执行多次这个代码时,导致连接无法释放
查看端口效果如下:
HttpWebRequest发送post请求有关问题
求大神指教啊,急啊,当什么都不发的时候,循环多次没有任何问题



给你 用我写的完整的

 public class Http
    {
        static Encoding m_Encoding = Encoding.Default;

        public static string Get(string url)
        {
            return Proc(url, string.Empty);
        }



        public static string Post(string url, string data)
        {
            return Proc(url, data);
        }


        public static string Get(string url, List<NameValue> nameValues)
        {

            if (nameValues != null && nameValues.Count > 0)
            {
                string data = string.Join("&", nameValues.Select(o => string.Format("{0}={1}", o.Name, o.Value)).ToArray());
                url = string.Format("{0}?{1}", url, data);
            }
            return Get(url);
        }

        public static string Post(string url, List<NameValue> nameValues)
        {
            string data = string.Empty;
            if (nameValues != null && nameValues.Count > 0)
            {
                data = string.Join("&", nameValues.Select(o => string.Format("{0}={1}", o.Name, o.Value)).ToArray());
            }
            return Post(url, data);
        }

        private static HttpWebRequest CreateRequest(string url, string postData)
        {
            HttpWebRequest request = null;
            request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "*/*";
            request.ProtocolVersion = HttpVersion.Version11;
            request.AllowAutoRedirect = true;
            request.Timeout = 1000 * 30;
            request.KeepAlive = true;
            if (!string.IsNullOrEmpty(postData))
            {
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "POST";
                byte[] bytes = m_Encoding.GetBytes(postData);
                request.ContentLength = bytes.Length;
                Stream requestStream = null;
                try
                {
                    requestStream = request.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);
                }


                catch (Exception exception)
                {

                }
                finally
                {
                    if (requestStream != null)
                    {
                        requestStream.Close();
                    }
                }
            }
            return request;
        }


        private static string Proc(string url, string postData)
        {
            HttpWebRequest request = CreateRequest(url, postData);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), m_Encoding))
            {
                string reHtml = reader.ReadToEnd();
                reader.Close();
                return reHtml;
            }
        }
    }

    public class NameValue
    {
        public NameValue(string name, string value)
        {
            this.Name = name;
            this.Value = value;
        }
        public string Name { get; set; }
        public string Value { get; set; }
    }


DEMO

            List<NameValue> list = new List<NameValue>();
            list.Add(new NameValue("AreaID", macInfo.AreaID));
            list.Add(new NameValue("Code", macInfo.Code));
            list.Add(new NameValue("SortIndex", (macInfo.Index + 1).ToString()));
            list.Add(new NameValue("Table", table));
            list.Add(new NameValue("Slot", slot));
            list.Add(new NameValue("SubSlot", subSlot));
            list.Add(new NameValue("Lane", lane));

 Http.Post(url,list)就是了



记得记点结贴呀。
[解决办法]
改三楼的,也许这样就可以了
 private static string Proc(string url, string postData)
        {
            HttpWebRequest request = CreateRequest(url, postData);
            using( HttpWebResponse response = (HttpWebResponse)request.GetResponse()){
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), m_Encoding))
            {
                string reHtml = reader.ReadToEnd();
                reader.Close();
                return reHtml;


            }
        }}



热点排行