C#使用使用post发送和接收数据的方法发送和接收数据的方法
主要介绍了C#使用post发送和接收数据的方法,涉及C#使用post收发数据的相关技巧,非常具有实用价值,需要的
朋友可以参考下
本文实例讲述了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++)
{
评论0