C#中调用Servlet示例
需求
通用消息接口使用servlet作为服务器端服务接口,第三方应用程序通过http post的方式调用servlet,实现与通用消息接口的调用连接。
参数说明如下:
msgTitle:消息标题,描述发送消息的标题
serviceId:服务编号,消息的服务编号
msgDesp:消息描述,消息的详细内容
msgURL:URL地址,消息中包含的 URL
上述4个参数的参数值可以为空,但参数必须提供。
调用示例
下面一段简单的html代码,描述了如何通过网页进行调用通用消息接口的模式,供参考。
<html><head><title>Sametime通用消息服务</title>
</head><body>
<formaction="http://stproxy.test.foton.com:9080/
fotonstbot/ServiceServlet" method="post">name="msgTitle" /><br />
服务编号: <input type="text" id="serviceId" name="serviceId" /><br />
消息描述: <input type="text" id="msgDesp" name="msgDesp" /><br />
URL: <input type="text" id="msgUrl" name="msgUrl" /><br />
系统类型: <input type="text" id="sysType" name="sysType" /><br />
目标用户: <input type="text" id="targetuser" name="targetuser" style="width:500px;" /><br />
<input type="submit" value="Submit" />
</form>
</body></html>
C#调用示例
/// <summary>
/// sametime
/// </summary>
/// <param name="bstrReceiver">sametime用户</param>
/// <param name="bstrTitle">标题</param>
/// <param name="lDelayTime">响应时间</param>
/// <param name="bstrMsg">内容</param>
public static void SendNotify(string bstrReceiver, string bstrTitle, int lDelayTime, string bstrMsg)
{
string user="";
try {
String url = "http://stproxy.foton.com.cn:9081/fotonstbot/ServiceServlet";//html调用的地址
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
if(webrequest==null)
{
RecorderErrorRtx(bstrReceiver,bstrTitle,bstrMsg,"服务端异常",0);
return;
}
webrequest.Method = "POST";
webrequest.Timeout = lDelayTime;
webrequest.ContentType = "application/x-www-form-urlencoded";
byte[] bufferTitile = Encoding.GetEncoding("utf-8").GetBytes(bstrTitle);
string sbTitle = "";
foreach (byte b in bufferTitile) sbTitle=sbTitle+(string.Format("%{0:X}", b));
byte[] bufferContent = Encoding.GetEncoding("utf-8").GetBytes(bstrMsg);
string sbContent = "";
//UTF8注意转码
foreach (byte b in bufferContent) sbContent=sbContent+(string.Format("%{0:X}", b));
System.Collections.Hashtable pars=new System.Collections.Hashtable();
pars.Add("msgTitle", sbTitle);
pars.Add("serviceId", "");
pars.Add("msgDesp", sbContent);
pars.Add("msgUrl", "");
pars.Add("sysType", "QCTS");
user="uid="+bstrReceiver+",cn=users,DC=FOTON;";
pars.Add("targetuser", user);
string buffer="";
//发送POST数据
if (!(pars == null || pars.Count == 0))
{
foreach (string key in pars.Keys)
{
buffer=buffer+"&"+key+"="+pars[key].ToString();
}
byte[] data = Encoding.UTF8.GetBytes(buffer);
using (Stream stream = webrequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
string[] values = webrequest.Headers.GetValues("Content-Type");
WebResponse myResponse= webrequest.GetResponse();
using(Stream resStream = myResponse.GetResponseStream())//得到回写的流
{
StreamReader newReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string Content = newReader.ReadToEnd();
newReader.Close();
}
RecorderRtx(user,bstrTitle,bstrMsg,0);
}
catch(Exception ex)
{
RecorderErrorRtx(user,bstrTitle,bstrMsg,ex.Message,0);
}
}