发送请求_发送soap请求调用wsdl服务
需求:在客戶端發起流程后,向另一個OA系統中發送一條代辦服務的通知
Web系統提供了wsdl
1,獲取對方web服務的地址:
處理方式,獲取我方系統的服務器路徑,然后在路徑下添加文件配置對方web系統的調用地址
string?path = System.AppDomain.CurrentDomain.BaseDirectory;
path += "JJWOA.txt";(這種方式很不標準了)
2,構建soap請求信息,按照對方提供的wsdl中的參數內容
//構造soap請求信息StringBuilder?soap?=?new?StringBuilder();soap.Append("<soapenv:Envelope?xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"?xmlns:web=\"http://webservice.notify.sys.kmss.landray.com/\">");soap.Append("<soapenv:Header/>");soap.Append("<soapenv:Body>");soap.Append("<web:sendTodo>");soap.Append("<arg0>");soap.Append("<appName>XX系統appName>");soap.Append("<createTime>");soap.Append(DateTime.Now.ToString("yyyy-MM-dd?HH:mm:ss"));soap.Append("createTime>");soap.Append("<docCreator>{\"LoginName\":\"");//發起人soap.Append(send);soap.Append("\"}docCreator>");soap.Append("<extendContent>extendContent>");soap.Append("<key>2key>");soap.Append("<level>3level>");soap.Append("<link>");soap.Append(prePath);soap.Append("link>");soap.Append("<modelId>NotifymodelId>");soap.Append("<modelName>流程通知modelName>");soap.Append("<others>others>");soap.Append("<param1>param1>");soap.Append("<param2>param2>");soap.Append("<subject>");soap.Append(topic);soap.Append("subject>");soap.Append("<targets>{\"LoginName\":\"");soap.Append(user);soap.Append("\"}targets>");soap.Append("<type>1type>");soap.Append("arg0>");soap.Append("web:sendTodo>");soap.Append("soapenv:Body>"); soap.Append("soapenv:Envelope>");3,消息中有一個link標簽,是對消息添加的內容,由于只是一句具體的提示內容(類似xx發起的xxx內容的流程),所以內容直接拼接在了url中
prePath += "/third/plm/todoMessage.html?type="?+ content;
prePath = prePath.Replace("\n", "").Replace("\t", "").Replace("\r", "");
prePath = InitChineseUrl(prePath);
方法InitChinesUrl用來處理url中中文亂碼問題,否則對方接受到的內容是無法正常顯示
public?string?InitChineseUrl(string?chineseUrl)????????{????????? Uri?url?=?new?Uri(chineseUrl);????????? System.Collections.Specialized.NameValueCollection?nv?=?System.Web.HttpUtility.ParseQueryString(url.Query,?System.Text.Encoding.GetEncoding("UTF-8"));????????????string?query?=?"";????????????for?(int?i?=?0;?i?????????????{????????????????if?(query.Trim()?==?string.Empty)????????????????{????????????????????query?=?"?"?+?nv.Keys[i]?+?"="?+?HttpUtility.UrlEncode(nv[i],?System.Text.Encoding.GetEncoding("UTF-8"));????????????????}????????????????else????????????????{????????????????????query?+=?"&"?+?nv.Keys[i]?+?"="?+?HttpUtility.UrlEncode(nv[i],?System.Text.Encoding.GetEncoding("UTF-8"));????????????????}????????????}????????????string?u?=?chineseUrl.Split('?')[0]?+?query;????????????u?=?u.Replace("+",?"%20");???????????????????????return?u; }4,發送請求
/// /// 發送SOAP請求,并返回響應xml /// /// 請求地址 /// SOAP請求信息 /// 返回響應信息 public static string GetSOAPReSource(string url, string datastr, string topic, string logId,string prePath,string logpath) { try { //發起請求 Uri uri = new Uri(url); WebRequest webRequest = WebRequest.Create(uri); webRequest.ContentType = "text/xml; charset=utf-8"; webRequest.Method = "POST"; using (Stream requestStream = webRequest.GetRequestStream()) { byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString()); requestStream.Write(paramBytes, 0, paramBytes.Length);????????????????} //響應 WebResponse webResponse = webRequest.GetResponse(); using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { string result = ""; FileStream fslog = new FileStream(logpath, FileMode.Append); StreamWriter swlog = new StreamWriter(fslog); swlog.WriteLine("Response信息:" + DateTime.Now.ToString() + ":" + logId + ":" + topic + ":" + myStreamReader.ReadToEnd().ToString()); swlog.Close(); return result = myStreamReader.ReadToEnd(); } } catch (Exception e) { throw; }????????}5,對應用戶的問題
發送的消息需要發送給客戶端流程中指定的用戶,由于兩邊系統中用戶登陸標識不一定完全相同,所以在客戶端中添加資源映射表,添加兩個系統中的用戶映射
代碼中添加的對于消息發送人和接收人的對應
至此,大功告成了...
總結
以上是生活随笔為你收集整理的发送请求_发送soap请求调用wsdl服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日志级别_Feign:请求压缩amp;日
- 下一篇: python sql 日期查询_Pyth