POST发送XML数据(短信业务)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace 发送xml
{
class Program
{
static void Main(string[] args)
{
//这是我需要发送的地址,我在http://localhost:6930/ajax/Receiveresponse需要接受到xml数据然后进行相应的处理。
string url2 = "http://localhost:6930/ajax/Receiveresponse";
//读取xml文件内容
FileStream fs = new FileStream(@"d:/test1.xml", FileMode.Open);
string xml="";
using (StreamReader sr = new StreamReader(fs))
{
xml = sr.ReadToEnd();
}
Console.WriteLine(PostData(url2, xml));
Console.ReadKey();
}
public static string PostData(string purl, string str)
{
try
{
byte[] data = System.Text.Encoding.GetEncoding("GB2312").GetBytes(str);
// 准备请求
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(purl);
//设置超时
req.Timeout = 30000;
req.Method = "Post";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
Stream stream = req.GetRequestStream();
// 发送数据
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
Stream receiveStream = rep.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
StringBuilder sb = new StringBuilder("");
while (count > 0)
{
String readstr = new String(read, 0, count);
sb.Append(readstr);
count = readStream.Read(read, 0, 256);
}
rep.Close();
readStream.Close();
return sb.ToString();
}
catch (Exception ex)
{
return "";
// ForumExceptions.Log(ex);
}
}
}
}
在另一个页面接受xml数据。我的xml文件格式如下
<?xml version="1.0" encoding="gb2312" ?>
<SBMP_MO_MESSAGE>
<CONNECT_ID>100</CONNECT_ID>
<MO_MESSAGE_ID>123456</MO_MESSAGE_ID>
<RECEIVE_DATE>20040605</RECEIVE_DATE>
<RECEIVE_TIME>153020</RECEIVE_TIME>
<GATEWAY_ID>1</GATEWAY_ID>
<VALID>1</VALID>
<CITY_CODE>010</CITY_CODE>
<CITY_NAME>北京</CITY_NAME>
<STATE_CODE>010</STATE_CODE>
<STATE_NAME>北京</STATE_NAME>
<TP_PID>0</TP_PID>
<TP_UDHI>0</TP_UDHI>
<MSISDN>18801372511</MSISDN>
<MESSAGE_TYPE>8</MESSAGE_TYPE>
<MESSAGE>801963</MESSAGE>
<LONG_CODE>100</LONG_CODE>
<SERVICE_CODE>9588</SERVICE_CODE>
</SBMP_MO_MESSAGE>
public ActionResult Receiveresponse()
{
string Result = "Ok";
//获取xml文件的输入流
Stream inputStream = Request.InputStream;
//定义字节数组
byte[] b = new byte[inputStream.Length];
//将输入流读入字节数组
inputStream.Read(b, 0, (int)inputStream.Length);
//将字节数组转换为经过编码的字符串
//string inputStr = System.Text.Encoding.UTF8.GetString(b);
string inputStr = System.Text.Encoding.Default.GetString(b);
Response.Write("ok"); //先返回ok告知短信业务平台已经接收到消息 然后再对此消息做异步处理
Result = ReceiveDataFromWapdm(inputStr);
return Content(Result);
}
private string ReceiveDataFromWapdm(string input)
{
string str = "ok";
try
{
//创建XmlDocument实例
XmlDocument x = new XmlDocument();
//加载 XML
x.LoadXml(input);
//获取根节点的名字
string name = x.DocumentElement.LocalName;
//判断获取的信息是否是mo消息
if (name.Equals("SBMP_MO_MESSAGE"))
{
ReceiveMoMessageClass m = new ReceiveMoMessageClass();
//获取SBMP_MO_MESSAGE
XmlNode node = x.SelectSingleNode("SBMP_MO_MESSAGE");
//以下是分析字节点
m.ConnectID = int.Parse(node.ChildNodes[0].InnerText);
m.MoMessageID = node.ChildNodes[1].InnerText;
m.ReceiveDate = node.ChildNodes[2].InnerText;
m.ReceiveTime = node.ChildNodes[3].InnerText;
m.GatewayID = int.Parse(node.ChildNodes[4].InnerText);
m.Valid = int.Parse(node.ChildNodes[5].InnerText);
m.CityCode = node.ChildNodes[6].InnerText;
m.CityName = node.ChildNodes[7].InnerText;
m.StateCode = node.ChildNodes[8].InnerText;
m.StateName = node.ChildNodes[9].InnerText;
m.TpPid = int.Parse(node.ChildNodes[10].InnerText);
m.TpUdhi = int.Parse(node.ChildNodes[11].InnerText);
m.Msisdn = node.ChildNodes[12].InnerText;
m.MessageType = int.Parse(node.ChildNodes[13].InnerText);
m.Message = node.ChildNodes[14].InnerText;
m.LongCode = node.ChildNodes[15].InnerText;
m.ServiceCode = node.ChildNodes[16].InnerText;
//在这里最好将获取的数据交给线程处理
Thread thread = new Thread(Run);
thread.IsBackground = true;
thread.Start(m);
//返回ok
}
else if (name.Equals("SBMP_REPORT_MESSAGE"))
{
}
//............
}
catch (Exception ex)
{
str = "false";
Console.WriteLine(ex.ToString());
}
return str;
}
/// <summary>
/// 短信上行信息类
/// </summary>
public class ReceiveMoMessageClass
{
private int _connectID = -1, _gatewayID = -1, _valid = -1, _tpPid = -1, _tpUdhi = -1,
_messageType = -1;
private string _moMessageID = "", _receiveTime = "", _receiveDate = "", _cityCode = "",
_cityName = "", _stateCode = "", _stateName = "", _msisdn = "", _message = "",
_longCode = "", _serviceCode = "";
/// <summary>
/// 消息类型
/// </summary>
public int MessageType
{
get { return _messageType; }
set { _messageType = value; }
}
/// <summary>
/// 短信业务平台接收到此MO消息的日期
/// </summary>
public string ReceiveDate
{
get { return _receiveDate; }
set { _receiveDate = value; }
}
/// <summary>
/// GSM协议
/// </summary>
public int TpUdhi
{
get { return _tpUdhi; }
set { _tpUdhi = value; }
}
/// <summary>
/// GSM协议
/// </summary>
public int TpPid
{
get { return _tpPid; }
set { _tpPid = value; }
}
/// <summary>
/// 此手机号码是否有效,0:无效,1:有效。
/// 无效的含义是此手机号码不在短信业务平台的号段内或者所属号段无效,此时使用此手机号码下发任何信息均失败。
/// 有效的含义是此手机号码在短信业务平台的号段内并且号段的状态为有效。
/// </summary>
public int Valid
{
get { return _valid; }
set { _valid = value; }
}
/// <summary>
/// 此MO消息是从哪个网关转发过来的
/// </summary>
public int GatewayID
{
get { return _gatewayID; }
set { _gatewayID = value; }
}
/// <summary>
/// 此MO信息是哪个连接标识的
/// </summary>
public int ConnectID
{
get { return _connectID; }
set { _connectID = value; }
}
/// <summary>
/// 用户发送此MO消息时的特符号
/// </summary>
public string ServiceCode
{
get { return _serviceCode; }
set { _serviceCode = value; }
}
/// <summary>
/// 用户发送此MO消息时的长号码
/// </summary>
public string LongCode
{
get { return _longCode; }
set { _longCode = value; }
}
/// <summary>
/// 消息内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
/// <summary>
/// 发送此MO信息的手机号码
/// </summary>
public string Msisdn
{
get { return _msisdn; }
set { _msisdn = value; }
}
/// <summary>
/// 手机号码所在省的名称,如果lValid为0,那么此值为空。
/// </summary>
public string StateName
{
get { return _stateName; }
set { _stateName = value; }
}
/// <summary>
/// 手机号码所在省的区号,如果lValid为0,那么此值为空。
/// </summary>
public string StateCode
{
get { return _stateCode; }
set { _stateCode = value; }
}
/// <summary>
/// 手机号码所在城市的名称,如果lValid为0,那么此值为空。
/// </summary>
public string CityName
{
get { return _cityName; }
set { _cityName = value; }
}
/// <summary>
/// 手机号码所在城市的区号,如果lValid为0,那么此值为空。
/// </summary>
public string CityCode
{
get { return _cityCode; }
set { _cityCode = value; }
}
/// <summary>
/// 短信业务平台接收到此MO消息的时间
/// </summary>
public string ReceiveTime
{
get { return _receiveTime; }
set { _receiveTime = value; }
}
/// <summary>
/// MO消息ID
/// </summary>
public string MoMessageID
{
get { return _moMessageID; }
set { _moMessageID = value; }
}
}