今天用(yòng)到之前封裝(zhuāng)的web request 請求,但抛錯:無法發送具(jù)有(yǒu)此謂詞類型的内容正文(wén)。
經過檢查,發現之前的方法裏面有(yǒu)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; }