C#中挪用Servlet示例。本站提示廣大學習愛好者:(C#中挪用Servlet示例)文章只能為提供參考,不一定能成為您想要的結果。以下是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" /><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);
}
}