今天用到之前封裝的web request 請求,但拋錯:無法發(fā)送具有此謂詞類型的內(nèi)容正文。
經(jīng)過檢查,發(fā)現(xiàn)之前的方法里面有GetRequestStream(), GET請求并不支持,于是把GetRequestStream()和相應(yīng)的代碼注釋掉就OK了
下面提供完整的c# web request 請求方法供參考
/// <summary> /// 請求url,并獲取返回值 /// </summary> /// <param name="postUrl">url地址</param> /// <param name="_Encode">Encoding 編碼</param> /// <param name="_Result">返回結(jié)果</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; }