本篇內容介紹了“C#中C/S端如何實現WebService服務”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
使用 C#以B/S方式構建WebService服務十分簡便,即是使用Asp.net在網站中添加WebService服務并使用IIS發布。但如需要在C/S程序中發布WebService服務則沒有直接可用的類庫。因此需要使用另外的方式實現WebService服務。
WebService實際是使用Http并遵循SOAP協議格式進行交互。能夠進行Http通訊即可實現WebService服務,只是沒了現成的類庫就需要自己編寫解析SOAP格式數據包和組織應答包。
代碼如下(示例):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Net; using System.Web; namespace LadarManufacturabilityTooling { public class HttpServic { public delegate byte[] OnGetResponseDataHandle(HttpListenerPostValue Sender); public event OnGetResponseDataHandle OnGetResponse; private static HttpListener httpPostRequest = new HttpListener(); private static bool IsRun = true; public HttpServic(IPAddress HttpServerIP, int HttpServerPort) { httpPostRequest.Prefixes.Add("http://" + HttpServerIP.ToString() + ":" + HttpServerPort.ToString() + "/"); try { httpPostRequest.Start(); } catch(Exception ex) { string Mes = ex.Message; } Thread ThrednHttpPostRequest = new Thread(new ThreadStart(httpPostRequestHandle)); ThrednHttpPostRequest.Start(); } private void httpPostRequestHandle() { while (IsRun) { try { HttpListenerContext requestContext = httpPostRequest.GetContext(); Thread threadsub = new Thread(new ParameterizedThreadStart((requestcontext) => { HttpListenerContext request = (HttpListenerContext)requestcontext; //獲取Post請求中的參數和值幫助類 HttpListenerPostParaHelper httppost = new HttpListenerPostParaHelper(request); //獲取Post過來的參數和數據 HttpListenerPostValue lst = httppost.GetHttpListenerPostValue(); byte[] buffer = null; if (lst != null) { if(OnGetResponse != null) buffer = OnGetResponse(lst); } if(buffer != null) {//Response try { request.Response.StatusCode = 200; request.Response.Headers.Add("SOAPAction", ""); request.Response.Headers.Add("User-Agent", "gSOAP/2.8"); request.Response.ContentType = "text/xml; charset=utf-8"; request.Response.ContentEncoding = Encoding.UTF8; request.Response.ContentLength74 = buffer.Length; var output = request.Response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); } catch(Exception ex2) { } } else { try { request.Response.Close(); } catch { } } })); threadsub.Start(requestContext); } catch (Exception ex) { string Mes = ex.Message; } } } public void StopHttpThread() { IsRun = false; httpPostRequest.Abort(); } } }
啟動服務后在httpPostRequestHandle()函數中編寫對監聽到的服務請求的處理。
//獲取Post過來的參數和數據 HttpListenerPostValue lst = httppost.GetHttpListenerPostValue();
GetHttpListenerPostValue();函數作用為取出請求中的數據部分和請求的名稱。涉及到的類定義和代碼如下:
/// <summary> /// HttpListenner監聽Post請求參數值實體 /// </summary> public class HttpListenerPostValue { /// <summary> /// 0=> 參數 /// 1=> 文件 /// </summary> public int type = 0; /// <summary> /// 請求的類型名稱 /// </summary> public string name; /// <summary> /// 數據字符串 /// </summary> public string datas; }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Web; using System.IO; namespace LadarManufacturabilityTooling { /// <summary> /// 獲取Post請求中的參數和值幫助類 /// </summary> public class HttpListenerPostParaHelper { private HttpListenerContext request; public HttpListenerPostParaHelper(HttpListenerContext request) { this.request = request; } /// <summary> /// 獲取Post過來的參數和數據 /// </summary> /// <returns></returns> public HttpListenerPostValue GetHttpListenerPostValue() { try { HttpListenerPostValue HttpListenerPostValueList = new HttpListenerPostValue(); if (true) { Stream body = request.Request.InputStream; Encoding encoding = Encoding.UTF8; StreamReader reader = new System.IO.StreamReader(body, encoding); if (request.Request.ContentType != null) { Console.WriteLine("Client data content type {0}", request.Request.ContentType); } string datas = reader.ReadToEnd(); string Requestname = request.Request.RawUrl.Replace("/",""); HttpListenerPostValueList.datas = datas; HttpListenerPostValueList.name = Requestname; Console.WriteLine(datas); } return HttpListenerPostValueList; } catch (Exception ex) { return null; } } } }
以上部分和構建普通的http監聽服務并無區別。
OnGetResponse事件用于處理請求的數據并組織回包
代碼如下(示例):
private byte[] ThisHttpServic_OnGetResponse(HttpListenerPostValue Sender) { byte[] buffer = null; string restr = ""; //處理收到的請求 switch (Sender.name) { case "MyServiceName": { string xmlOrgstr = ""; int iStartPos = Sender.datas.IndexOf("<xmlData>", 1); int iStopPos = Sender.datas.IndexOf("</xmlData>", 1); if (iStartPos > 0) { xmlOrgstr = Sender.datas.Substring(iStartPos + 9, iStopPos - iStartPos - 9); } string xmlstr = HttpUtility.HtmlDecode(xmlOrgstr); string LOGIN_ACK = GetPack(xmlstr); restr = GetCompleteSoapString(System.Security.SecurityElement.Escape(LOGIN_ACK)); break; } default: restr = ""; break; } buffer = System.Text.Encoding.UTF8.GetBytes(restr); return buffer; }
需要從收到的http請求的數據部分提取出WebService服務的參數。
<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:client1="http://LSCService.chinamobile.com" xmlns:service1="http://FSUService.chinamobile.com"> <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <client1:invoke> <xmlData><?xml version="1.0" encoding="UTF-8" ?><Request><PK_Type><Name>LOGIN</Name></PK_Type><Info><UserName>cmcc</UserName><PassWord>B101341CC2E4D6F5B395C7544B96A826</PassWord><FSUID>21202110060001</FSUID><FSUIP>192.168.1.253</FSUIP><FSUMAC>00:21:92:01:b5:9f</FSUMAC><FSUVER>2.0.0.15 for CMCC</FSUVER></Info></Request>
 </xmlData> </client1:invoke>< /SOAP-ENV:Body> </SOAP-ENV:Envelope>
收到的數據包原文(Sender.datas)為:
作為示例的服務的參數名為xmlData從SOAP中截取出參數的字符串進行處理。
由于xmlData中的內容是一串xml字符,SOAP傳輸時經過了轉義,因此還需要轉義回來。
string xmlstr = HttpUtility.HtmlDecode(xmlOrgstr);
處理完相應的業務,將需要回復的數據加上SOAP協議的頭尾組好回復包返回。需要轉義的部分記得進行符號轉義。
System.Security.SecurityElement.Escape(LOGIN_ACK)
SOAP協議的頭尾根據WebService服務函數的定義有所不同,需要自行組織。示例如下:
/// <summary> /// 返回完整的SOAP包 /// </summary> /// <param name="XmlData">應答部分</param> /// <returns></returns> public static string GetCompleteSoapString(string XmlData) { string restr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"" + " xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" + " xmlns:client1=\"http://LService.mobile.com\"" + " xmlns:service1=\"http://FService.mobile.com\">" + "<SOAP-ENV:Body>" + "<client1:invokeResponse><invokeReturn>"; string restrEnd = "</invokeReturn></client1:invokeResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>"; restr = restr + XmlData + restrEnd; return restr; }
“C#中C/S端如何實現WebService服務”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。