亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

實例詳解C#實現http不同方法的請求

發布時間:2020-09-11 23:40:03 來源:腳本之家 閱讀:183 作者:郝光明 欄目:編程語言

1、不帶參數的POST請求

public static string Post(string url) 
{ 
  string result = ""; 
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
  req.Method = "POST"; 
  HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
  Stream stream = resp.GetResponseStream(); 
  //獲取內容 
  using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) 
  { 
    result = reader.ReadToEnd(); 
  } 
  return result; 
} 

2、帶參數的POST請求

public static string Post(string url,Dictionary<string,string> dic) 
{ 
  string result = ""; 
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
  req.Method = "POST"; 
  req.ContentType = "application/x-www-form-urlencoded"; 
  #region 添加Post 參數 
  StringBuilder builder = new StringBuilder(); 
  int i = 0; 
  foreach (var item in dic) 
  { 
    if (i > 0) 
      builder.Append("&"); 
    builder.AppendFormat("{0}={1}", item.Key, item.Value); 
    i++; 
  } 
  byte[] data = Encoding.UTF8.GetBytes(builder.ToString()); 
  req.ContentLength = data.Length; 
  using (Stream reqStream = req.GetRequestStream()) 
  { 
    reqStream.Write(data, 0, data.Length); 
    reqStream.Close(); 
  } 
  #endregion 
  HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
  Stream stream = resp.GetResponseStream(); 
  //獲取響應內容 
  using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) 
  { 
    result = reader.ReadToEnd(); 
  } 
  return result; 
} 

3、不帶參數的GET請求

public static string Get(string url) 
{ 
  string result = ""; 
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
  HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
  Stream stream = resp.GetResponseStream(); 
  try 
  { 
    //獲取內容 
    using (StreamReader reader = new StreamReader(stream)) 
    { 
      result = reader.ReadToEnd(); 
    } 
  } 
  finally 
  { 
    stream.Close(); 
  } 
  return result; 
} 

4、帶參數的GET請求

public static string Get(string url, Dictionary<string, string> dic) 
{ 
  string result = ""; 
  StringBuilder builder = new StringBuilder(); 
  builder.Append(url); 
  if (dic.Count > 0) 
  { 
    builder.Append("?"); 
    int i = 0; 
    foreach (var item in dic) 
    { 
      if (i > 0) 
        builder.Append("&"); 
      builder.AppendFormat("{0}={1}", item.Key, item.Value); 
      i++; 
    } 
  } 
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString()); 
  //添加參數 
  HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
  Stream stream = resp.GetResponseStream(); 
  try 
  { 
    //獲取內容 
    using (StreamReader reader = new StreamReader(stream)) 
    { 
      result = reader.ReadToEnd(); 
    } 
  } 
  finally 
  { 
    stream.Close(); 
  } 
  return result; 
} 

5、自定義HTTP請求頭

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
req.Method = "GET"; 
req.Headers["Accept-Language"] = "zh-CN,zh;q=0.8"; 
req.Referer = "https://www.baidu.com/"; 
HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
Stream stream = resp.GetResponseStream(); string result = ""; //注意,此處使用的編碼是:gb2312 //using (StreamReader reader = new StreamReader(stream, Encoding.Default)) using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("gb2312"))) 
{ 
  result = reader.ReadToEnd(); 
} 

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

玛曲县| 安西县| 沁水县| 平陆县| 清丰县| 延长县| 彩票| 沂源县| 宣威市| 内丘县| 交城县| 巴林右旗| 迭部县| 嘉荫县| 南和县| 凉城县| 平度市| 南江县| 太谷县| 乐安县| 石首市| 河源市| 米林县| 墨玉县| 阿合奇县| 灌云县| 共和县| 吐鲁番市| 永善县| 沧源| 陇南市| 石屏县| 天柱县| 随州市| 湖南省| 三门县| 巴里| 青岛市| 江源县| 南木林县| 聂拉木县|