欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

实例详解C#实现http不同方法的请求

程序员文章站 2023-12-14 15:31:28
1、不带参数的post请求 public static string post(string url) { string result = "";...

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(); 
} 

上一篇:

下一篇: