91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

實例詳解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

防城港市| 承德市| 厦门市| 天气| 衡东县| 托里县| 额敏县| 河曲县| 昌图县| 海宁市| 柘城县| 城固县| 阳新县| 资兴市| 宁国市| 新郑市| 拜泉县| 苏尼特右旗| 盱眙县| 平泉县| 台前县| 琼结县| 龙岩市| 宁陕县| 铜鼓县| 灵山县| 平凉市| 桐庐县| 广饶县| 夏津县| 都江堰市| 阜新| 且末县| 隆化县| 巴林左旗| 宁安市| 通化县| 绥芬河市| 温州市| 嘉峪关市| 抚顺县|