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

C#使用post发送和接收数据的方法

程序员文章站 2022-08-03 21:40:55
本文实例讲述了c#使用post发送和接收数据的方法。分享给大家供大家参考。具体实现方法如下: public partial class post_server :...

本文实例讲述了c#使用post发送和接收数据的方法。分享给大家供大家参考。具体实现方法如下:

public partial class post_server : system.web.ui.page
{
 protected void page_load(object sender, eventargs e)
 {
  string type = "";
  string re = "";
  re += "数据传送方式:";
  if (request.requesttype.toupper() == "post")
  {
   type = "post";
   re += type + "<br/>参数分别是:<br/>";
   sortedlist table = param();
   if (table != null)
   {
    foreach (dictionaryentry de in table) {
     re += "参数名:" + de.key + " 值:" + de.value + "<br/>";
    }
   }
   else
   { re = "你没有传递任何参数过来!"; }
  }
  else
  {
   type = "get";
   re += type + "<br/>参数分别是:<br/>";
   namevaluecollection nvc = getinput();
   if (nvc.count != 0)
   {
    for (int i = 0; i < nvc.count; i++){
    re += "参数名:"+nvc.getkey(i)+"值:"+nvc.getvalues(i)[0]+"<br/>";
 }
   }
   else
   { re = "你没有传递任何参数过来!"; }
  }
  response.write(re);
 }
 //获取get返回来的数据
 private namevaluecollection getinput()
 { return request.querystring; }
 // 获取post返回来的数据
 private string postinput()
 {
  try
  {
   system.io.stream s = request.inputstream;
   int count = 0;
   byte[] buffer = new byte[1024];
   stringbuilder builder = new stringbuilder();
   while ((count = s.read(buffer, 0, 1024)) > 0)
   {
    builder.append(encoding.utf8.getstring(buffer,0,count));
   }
   s.flush();
   s.close();
   s.dispose();
   return builder.tostring();
  }
  catch (exception ex)
  { throw ex; }
 }
 private sortedlist param()
 {
  string poststr = postinput();
  sortedlist sortlist = new sortedlist();
  int index = poststr.indexof("&");
  string[] arr = { };
  if (index != -1) //参数传递不只一项
  {
   arr = poststr.split('&');
   for (int i = 0; i < arr.length; i++)
   {
    int equalindex = arr[i].indexof('=');
    string paramn = arr[i].substring(0, equalindex);
    string paramv = arr[i].substring(equalindex + 1);
    if (!sortlist.containskey(paramn))
    //避免用户传递相同参数
    { sortlist.add(paramn, paramv); }
    else //如果有相同的,一直删除取最后一个值为准
    {
     sortlist.remove(paramn); sortlist.add(paramn, paramv);
    }
   }
  }
  else //参数少于或等于1项
  {
   int equalindex = poststr.indexof('=');
   if (equalindex != -1)
   { //参数是1项
    string paramn = poststr.substring(0, equalindex);
    string paramv = poststr.substring(equalindex + 1);
    sortlist.add(paramn, paramv);
   }
   else //没有传递参数过来
   { sortlist = null; }
  }
  return sortlist;
 }
}
protected void button1_click(object sender, eventargs e)
{
  encoding encode = system.text.encoding.getencoding("utf-8");
  byte[] arrb = encode.getbytes("aa=aa&bb=好飞");
  httpwebrequest myreq = (httpwebrequest)webrequest.create("http://localhost:11626/mytest/post_server.aspx");
  myreq.method = "post";
  myreq.contenttype = "application/x-www-form-urlencoded";
  myreq.contentlength = arrb.length;
  stream outstream = myreq.getrequeststream();
  outstream.write(arrb, 0, arrb.length);
  outstream.close();
  //接收http做出的响应
  webresponse myresp = myreq.getresponse();
  stream receivestream = myresp.getresponsestream();
  streamreader readstream = new streamreader(receivestream, encode);
  char[] read = new char[256];
  int count = readstream.read(read, 0, 256);
  string str = null;
  while (count > 0)
  {
   str += new string(read, 0, count);
   count = readstream.read(read, 0, 256);
  }
  readstream.close();
  myresp.close();
  response.write(str);
}

希望本文所述对大家的c#程序设计有所帮助。