2008年8月11日星期一

.Net实现Post方式数据传输

这两天帮一个朋友实现一个短信交互的网站,要跟sp进行接口数据交换,接口实现方式是Post,我们提供数据接收,实现功能后自己也写了一段POST程序进行了一下测试,代码如下:
首先是发送数据

string strParm = "传输的XML字符串";
Encoding encode = System.Text.Encoding.Default;

byte[] arrB = encode.GetBytes(strParm);

HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://localhost:34738/MOPage.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();
WebResponse myResp = null;
try
{
//接收HTTP做出的响应
myResp = myReq.GetResponse();
}
catch (Exception ex)
{
throw ex;
}
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);


下边代码实现接收数据功能


由于数据编码格式采用的GBK,接口使用asp.net技术实现,所以接收端采用GB2312格式转换数据流,接收数据并转换为xml实体对象存储
string ReqToWrite = string.Empty;
try
{
byte[] ReqData = Request.BinaryRead(Request.ContentLength);//得到请求的数据
//string ReqQuery = System.Text.UnicodeEncoding.UTF8.GetString(ReqData, 0, ReqData.Length);
string ReqQuery = System.Text.Encoding.GetEncoding("GB2312").GetString(ReqData, 0, ReqData.Length);
ReqQuery = ReqQuery.Replace("\r\n", "");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(ReqQuery);
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList nodeList = root.SelectNodes("/message");
int i=0;
foreach (XmlNode node in nodeList)
{
string linkid, spid, spcode, feecode, feeprice, toicp, product, feecategory, channel, mobile, content, isprovision, issubscribe, createDate;
linkid = node.SelectSingleNode("linkid").InnerText.Trim();
spid = node.SelectSingleNode("spid").InnerText.Trim();
spcode = node.SelectSingleNode("spcode").InnerText.Trim();
feecode = node.SelectSingleNode("feecode").InnerText.Trim();
feeprice = node.SelectSingleNode("feeprice").InnerText.Trim();
toicp = node.SelectSingleNode("toicp").InnerText.Trim();
product = node.SelectSingleNode("product").InnerText.Trim();
feecategory = node.SelectSingleNode("feecategory").InnerText.Trim();
channel = node.SelectSingleNode("channel").InnerText.Trim();
mobile = node.SelectSingleNode("mobile").InnerText.Trim();
content = node.SelectSingleNode("content").InnerText.Trim();
isprovision = node.SelectSingleNode("isprovision").InnerText.Trim();
issubscribe = node.SelectSingleNode("issubscribe").InnerText.Trim();
createDate = node.SelectSingleNode("createDate").InnerText.Trim();
i=InterfaceAccess.InsertMOInfo(linkid, spid, spcode, feecode, Convert.ToInt32(feeprice), toicp, product, feecategory,
channel, mobile, content, isprovision, issubscribe, Convert.ToDateTime(createDate), ReqQuery);
}
ReqToWrite = i.ToString();
}
catch (Exception ex)
{
// ....
}
finally
{
Response.Write(ReqToWrite);
Response.End();
}

以上程序分别需要的命名空间引用如下:
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
有一点需要要注意ASP.NET中为了防止注入攻击,对"<"等字符进行了限制,所以要在被请求的ASPX页面中,将 Page指令中的ValidateRequest 设置"false"

没有评论: