HttpWebRequest 设置参数时出现个诡异问题

.Net技术 码拜 8年前 (2016-03-08) 1328次浏览
在获取post流时,这句代码就把封包传送了 requst.GetRequestStream();.
执行这句的时候就没有发送封包 HttpWebResponse se = requst.GetResponse() as HttpWebResponse;

string url = "http://w.qzone.qq.com/cgi-bin/likes/internal_dolike_app?g_tk=" + _key;
            HttpWebRequest requst = WebRequest.Create(url) as HttpWebRequest;
            
            requst.Method = "POST";
            requst.Host = "w.qzone.qq.com";
            requst.AutomaticDecompression = DecompressionMethods.GZip;
            requst.KeepAlive = true;
            requst.Headers[HttpRequestHeader.AcceptLanguage] = "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3";
            requst.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
            requst.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0";
            requst.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            requst.ContentType = "application/x-www-form-urlencoded";
            //参数设置
            string shuoshuoid = "46c54c5998411257d4040200";
            byte[] btbyte = Encoding.UTF8.GetBytes("qzreferrer=http%3A%2F%2Fuser.qzone.qq.com%2F" + _qq_fuwu + "%2F"
                + "&opuin=" + _qq_fuwu
                + "&unikey=http:%3A%2F%2Fuser.qzone.qq.com%2F" + _qq_kehu + "%2Fmood%2F" + shuoshuoid
                + "&curkey=http:%3A%2F%2Fuser.qzone.qq.com%2F" + _qq_kehu + "%2Fmood%2F" + shuoshuoid
                + "&from=1"
                + "&appid=311"
                + "&typeid=0"
                + "&abstime=1460869123"
                + "&fid=" + shuoshuoid
                + "&active=0"
                + "&fupdate=1");
            requst.ContentLength = btbyte.Length;
            //获取流
//*****************主要问题出在这里,获取参数流时停顿了下,然后用封包拦截器,看了下在这里他就已经上传封包了*******
            Stream s = requst.GetRequestStream();
            s.Write(btbyte, 0, btbyte.Length);
            s.Close();
            
            
            try
            {
                HttpWebResponse se = requst.GetResponse() as HttpWebResponse;//然后这一句就没有任何停顿的执行过去了
                StreamReader streamReader;
                string responseContent;
                streamReader = new StreamReader(se.GetResponseStream(), Encoding.UTF8);
                responseContent = streamReader.ReadToEnd();
                Encoding.UTF8.GetString(Encoding.Default.GetBytes(responseContent));
            }
            catch (Exception)
            {
            }
解决方案

40

1、HttpWebResponse用完要Close以释放流和连接
2、没停顿过去了,是继续向下执行代码,还是进入catch了?

20

引用 2 楼 huiji5351 的回复:
Quote: 引用 1 楼 lovelj2012 的回复:

1、HttpWebResponse用完要Close以释放流和连接
2、没停顿过去了,是继续向下执行代码,还是进入catch了?

继续向下走没有引发异常.但是也没有发送任何数据

requst.GetResponse()是获取服务器应答,用streamReader.ReadToEnd()读取不到数据,说明服务器没有实质性应答咯

20

已经有了结果, 再看一下也不需要重新获取啊, LZ可以重新发起另一个请求.

20

要读取流的内容 是不是要把流的指针调回到开头位置呢

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明HttpWebRequest 设置参数时出现个诡异问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)