分享最实用的技术,创造更大的价值

c#无法发送具有此谓词类型的内容正文 异常

c#无法发送具有此谓词类型的内容正文

今天用到之前封装的web request 请求,但抛错:无法发送具有此谓词类型的内容正文。

经过检查,发现之前的方法里面有GetRequestStream(), GET请求并不支持,于是把GetRequestStream()和相应的代码注释掉就OK了


下面提供完整的c# web request 请求方法供参考

        /// <summary>
        /// 请求url,并获取返回值
        /// </summary>
        /// <param name="postUrl">url地址</param>
        /// <param name="_Encode">Encoding 编码</param>
        /// <param name="_Result">返回结果</param>
        /// <param name="_ErrMsg">错误时的错误消息</param>
        /// <returns></returns>
        public bool WebRequestData(string postUrl, Encoding _Encode, ref string _Result, ref string _ErrMsg)
        {
            bool bln = false;
            try
            {
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "GET";
                webReq.ContentType = "application/x-www-form-urlencoded";

                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                _Result = sr.ReadToEnd();

                bln = true;

                sr.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                _ErrMsg = ex.Message;
            }
            return bln;
        }


联系
QQ
电话
咨询电话:189-8199-7898
TOP