图1 Web Comm 组件工作原理
///
<summary>
///
发送消息
///
通过这个方法发送消息不进行加密
///
</summary>
///
<param name="url">Web Server的Url</param>
///
<param name="evt">消息事件号</param>
///
<param name="userName">消息归属的用户名</param>
///
<param name="msg">消息体</param>
///
<param name="response">Web Server 响应后返回的消息</param>
///
<exception cref="CCommException">出错触发CCommException异常</exception>
///
<remarks>
/// msg
---> web server
/// response <--- web server
///
<summary>
///
发送安全消息
///
通过这个方法发送的消息是进行过加密的。
///
</summary>
///
<param name="url">Web Server的Url</param>
///
<param name="evt">消息事件号</param>
///
<param name="userName">消息归属的用户名</param>
///
<param name="msg">消息体</param>
///
<param name="response">Web Server 响应后返回的消息</param>
///
<exception cref="CCommException">出错触发CCommException异常</exception>
///
<remarks>
/// msg
---> web server
/// response <--- web server
public void SendSecMsg(String url, int evt, object msg, out object response)
[Serializable]
public enum T_CommError
{
Success = 0,
///
<summary>
///
序列化错误
///
</summary>
SerializationFail = 100,
///
<summary>
///
反序列化
///
</summary>
DeserializationFail = 101,
///
<summary>
/// DES 加密错误
///
</summary>
DesEncryptFail = 102,
///
<summary>
/// DES 解密错误
///
</summary>
DesDecryptFail = 103,
///
<summary>
///
流转换为字符串错误
///
</summary>
StreamToStringFail = 104,
///
<summary>
///
字符串转换为流错误
///
</summary>
StringToStreamFail = 105,
///
<summary>
///
字符串到Base64错误
///
</summary>
StringToBase64Fail = 106,
///
<summary>
/// Base64到字符串错误
///
</summary>
Base64ToStringFail = 107,
///
<summary>
///
无效的响应
///
</summary>
InvalidResponse = 108,
///
<summary>
///
空的公钥
///
</summary>
EmptyPubkey
= 109,
///
<summary>
///
无效的公钥
///
</summary>
InvalidPubkey
= 110,
///
<summary>
///
无效的事件号
///
</summary>
InvalidEvent
= 111,
///
<summary>
///
连接错误
///
</summary>
ConnectFail = 500,
////////////////////////////////////////////////////////
//服务器侧错误
///
<summary>
///
读取参数错误
///
</summary>
SvrParaFail = 10000,
///
<summary>
/// RSA 解密错误
///
</summary>
RSAFail
= 10001,
///
<summary>
///
未知错误
///
</summary>
Unknow = 65535,
}
1、
由于发送消息过程需要对消息进行序列化和反序列化,所以需要被发送的消息必须可以被序列化,其实很简单,就是所有需要发送的消息类都要实现
[Serializable] 属性
2、
1000 以下的消息号为系统保留,发送消息时消息号必须大于等于1000
private void buttonSendMsg_Click(object sender, EventArgs e)
{
int msg = (int)numericUpDownData.Value;
object response;
try
{
m_CntWebComm.SendMsg(textBoxUrl.Text, 1200, msg, out response);
if (response != null)
{
if (response is T_SrvError)
{
T_SrvError error = (T_SrvError)response;
CMsgBox.ShowErrorMessageBox(String.Format("code={0}, errmsg={1}, stack:\r\n{2}",
error.Error, error.ErrMsg, error.StackTrace));
}
else
{
numericUpDownData.Value = (int)response;
}
}
}
catch (CCommException ce)
{
CMsgBox.ShowErrorMessageBox(String.Format("code={0}, errmsg={1}",
ce.ErrorCode, ce.Message));
}
catch (Exception e1)
{
CMsgBox.ShowErrorMessageBox(String.Format("errmsg={0}",
e1.Message));
}
}
private void buttonSendSecMsg_Click(object sender, EventArgs e)
{
int msg = (int)numericUpDownData.Value;
object response;
try
{
m_CntWebComm.SendSecMsg(textBoxUrl.Text, 1200, msg, out response);
if (response != null)
{
if (response is T_SrvError)
{
T_SrvError error = (T_SrvError)response;
CMsgBox.ShowErrorMessageBox(String.Format("code={0}, errmsg={1}, stack:\r\n{2}",
error.Error, error.ErrMsg, error.StackTrace));
}
else
{
numericUpDownData.Value = (int)response;
}
}
}
catch (CCommException ce)
{
CMsgBox.ShowErrorMessageBox(String.Format("code={0}, errmsg={1}",
ce.ErrorCode, ce.Message));
}
catch (Exception e1)
{
CMsgBox.ShowErrorMessageBox(String.Format("errmsg={0}",
e1.Message));
}
}