c#发送和接收get,post消息

c#后台获取post消息,get消息就用Request.QueryString[“key”]

if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST") {
    Stream bs = HttpContext.Current.Request.InputStream;
    byte[] buff = new byte[bs.Length];
    bs.Read(buff, 0, (Int32)bs.Length);
    bs.Close();
   return Encoding.UTF8.GetString(buff);
  }

发送post数据

  string url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
  string token = mainArg.get_Token();
   url += token;
   HttpWebRequest req =(HttpWebRequest) HttpWebRequest.Create(url);
   req.Method = "POST";//大小写不限
   string menuPath = HttpContext.Current.Server.MapPath(@"~/TemplePath/menu.txt");
       Stream ms =File.Open(menuPath, FileMode.OpenOrCreate);
       byte[] buff = new byte[ms.Length];
       ms.Read(buff, 0, (Int32)ms.Length);
       string param = Encoding.Default.GetString(buff);
       ms.Close();
       using (StreamWriter stream=new StreamWriter(req.GetRequestStream()))
       {
         stream.Write(param);
            }
       using (HttpWebResponse response=(HttpWebResponse)req.GetResponse())
         {
          Stream s = response.GetResponseStream();
          StreamReader sr = new StreamReader(s);
          string result = sr.ReadToEnd();
          sr.Close();
          return result;           
            }

get方式

   string token = mainArg.get_Token();
   string url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + token; ;
   HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
   using (HttpWebResponse response =(HttpWebResponse) req.GetResponse()) {
   StreamReader sr = new StreamReader(response.GetResponseStream());
   string result = sr.ReadToEnd();
   sr.Close();
   return result;        

你可能感兴趣的:(c#)