C# WebService 发送XML数据

    xiaoxiao2022-07-12  162

    1.编写SQL查询数据库

    sSql = "select * from 表名 where pushstate =0 and id='" + id + "' ";//条件 DataTable dt = dal.Session.Query(sSql.ToString()).Tables[0];

    2.拼接XML

    if (dt.Rows.Count > 0) { StringBuilder sendXml = new StringBuilder(); //XML固定头部 sendXml.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:jour=\"http://tbea/oracle/apps/ws/Journal.wsdl\" xmlns:typ=\"http://tbea/oracle/apps/ws/Journal.wsdl/types/\">"); sendXml.AppendFormat("<soapenv:Header/>"); sendXml.AppendFormat("<soapenv:Body>"); sendXml.AppendFormat("<jour:glJournalMain>"); //循环数据生成XML for (int i = 0; i < dt.Rows.Count; i++) { sendXmlM.AppendFormat("<typ:User>"); sendXmlM.AppendFormat("<typ:head>{0}</typ:head>", dt.Rows[0]["header"].ToString()); sendXmlM.AppendFormat("<typ:sourceTrx>{0}</typ:sourceTrx>", dt.Rows[0]["source"].ToString()); sendXmlM.AppendFormat("</typ:User>"); } sendXml.AppendFormat("</jour:glJournalMain>"); sendXml.AppendFormat("</soapenv:Body>"); sendXml.AppendFormat("</soapenv:Envelope>"); //输出日志 RecorderLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "发送XML:" + sendXml.ToString()); string url = "发送地址";//url string reponseXml = Send_XML(url, sendXml.ToString()); RecorderLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "返回XML:" + reponseXml);

     3.发送XML,Url获取返回信息

    //发送url public string Send_XML(string url, string sendXml) { string result = ""; byte[] bytes = Encoding.UTF8.GetBytes(sendXml); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = "POST"; httpWebRequest.ContentLength = (long)bytes.Length; httpWebRequest.ContentType = "text/xml;charset=UTF-8"; httpWebRequest.Headers.Add("content", "text/xml;charset=utf8"); try { Stream writer = httpWebRequest.GetRequestStream(); writer.Write(bytes, 0, bytes.Length); //关闭请求流 writer.Close(); } catch (Exception ex) { return ex.Message; } finally { //streamWriter.Close(); } HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); streamReader.Close(); } return result; }

     

     

    最新回复(0)