C#中调用Servlet示例
需求
通用消息接口使用servlet作为服务器端服务接口,第三方应用程序通过httppost的方式调用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/> 服务编号:<inputtype="text"id="serviceId"name="serviceId"/><br/> 消息描述: <inputtype="text"id="msgDesp"name="msgDesp"/><br/> URL: <inputtype="text"id="msgUrl"name="msgUrl"/><br/> 系统类型: <inputtype="text"id="sysType"name="sysType"/><br/> 目标用户: <inputtype="text"id="targetuser"name="targetuser"style="width:500px;"/><br/> <inputtype="submit"value="Submit"/> </form> </body></html>
C#调用示例
///<summary> ///sametime ///</summary> ///<paramname="bstrReceiver">sametime用户</param> ///<paramname="bstrTitle">标题</param> ///<paramname="lDelayTime">响应时间</param> ///<paramname="bstrMsg">内容</param> publicstaticvoidSendNotify(stringbstrReceiver,stringbstrTitle,intlDelayTime,stringbstrMsg) { stringuser=""; try{ Stringurl="http://stproxy.foton.com.cn:9081/fotonstbot/ServiceServlet";//html调用的地址 HttpWebRequestwebrequest=(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); stringsbTitle=""; foreach(bytebinbufferTitile)sbTitle=sbTitle+(string.Format("%{0:X}",b)); byte[]bufferContent=Encoding.GetEncoding("utf-8").GetBytes(bstrMsg); stringsbContent=""; //UTF8注意转码 foreach(bytebinbufferContent)sbContent=sbContent+(string.Format("%{0:X}",b)); System.Collections.Hashtablepars=newSystem.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); stringbuffer=""; //发送POST数据 if(!(pars==null||pars.Count==0)) {
foreach(stringkeyinpars.Keys) { buffer=buffer+"&"+key+"="+pars[key].ToString(); } byte[]data=Encoding.UTF8.GetBytes(buffer); using(Streamstream=webrequest.GetRequestStream()) { stream.Write(data,0,data.Length); } }
string[]values=webrequest.Headers.GetValues("Content-Type"); WebResponsemyResponse=webrequest.GetResponse();
using(StreamresStream=myResponse.GetResponseStream())//得到回写的流 { StreamReadernewReader=newStreamReader(myResponse.GetResponseStream(),Encoding.UTF8); stringContent=newReader.ReadToEnd(); newReader.Close(); } RecorderRtx(user,bstrTitle,bstrMsg,0);
}
catch(Exceptionex) { RecorderErrorRtx(user,bstrTitle,bstrMsg,ex.Message,0); } }