博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过HttpWebRequest调用webService
阅读量:6431 次
发布时间:2019-06-23

本文共 10578 字,大约阅读时间需要 35 分钟。

调用远端接口,向远端接口写入一个xml文件(loginLog为xml的字符串)。关键方法如下:

public object InsertAuditLog(string loginLog)        {                          //Wsdlxml(loginLog)返回wsdl的xml            byte[] bs = Encoding.UTF8.GetBytes(Wsdlxml(loginLog));            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://10.109.209.90:21010/gather/services/AioxPort");            myRequest.Method = "POST";            myRequest.ContentType = "text/xml; charset=utf-8";            //mediate为调用方法            myRequest.Headers.Add("SOAPAction", "http://tempuri.org/mediate");            myRequest.ContentLength = bs.Length;            //Console.WriteLine("完成准备工作");            using (Stream reqStream = myRequest.GetRequestStream())            {                reqStream.Write(bs, 0, bs.Length);            }            using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())            {                StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);                return sr.ReadToEnd();                //Console.WriteLine("反馈结果" + responseString);            }            //Console.WriteLine("完成调用接口");        }

 


 

自己项目中的完整应用代码如下(备忘):

using System;using System.Collections.Generic;using System.Linq;using System.Web;using  System.Data;using System.Collections;using System.Xml;using System.Net;using System.IO;using System.Text;namespace AuditLog{    public class AuditLogServer    {        ///         /// 将计件满足条件日志写入4A        ///         public void WriteAuditLogTo4A()        {            DateTime currTime = DateTime.Now;            #region 处理登录日志            string loginLog = "";            DataTable loginLogList = GetLoginLog(currTime);            if (loginLogList != null && loginLogList.Rows.Count > 0)            {                for (int i = 0; i < loginLogList.Rows.Count; i++)                {                    DataRow dr = loginLogList.Rows[i];                    //生成一条日志                    loginLog = Generate4ALogXml(dr,1);                    try                    {                        InsertAuditLog(loginLog);                        string updateSql = string.Format(@"update CC_LOGIN_HISTORY set tag=1 where ID={0}", Convert.ToInt32(dr["ID"].ToString()));                        ExecuteNonQuery(updateSql);//修改已写入4a登录日志                    }                    catch { }                    //if (i > 1)                    //{                    //    loginLogList = null;                    //    break;                    //}                }            }            #endregion            #region 处理模块操作日志            string moduleLog = "";            DataTable moduleLogList = GetMoudelLog(currTime);            if (moduleLogList != null && moduleLogList.Rows.Count > 0)            {                for (int i = 0; i < moduleLogList.Rows.Count; i++)                {                    DataRow dr = moduleLogList.Rows[i];                    moduleLog = Generate4ALogXml(dr, 2);                    try                    {                        InsertAuditLog(moduleLog);                        string updateSql = string.Format(@"update CC_OPERATION_LOG set tag=1 where ID={0}", Convert.ToInt32(dr["ID"].ToString()));                        ExecuteNonQuery(updateSql);//修改已写入4a登录日志                    }                    catch { }                    //if (i > 1)                    //{                    //    moduleLogList = null;                    //    break;                    //}                }            }            #endregion        }        ///         /// 向4a插入审计日志        ///         public object InsertAuditLog(string loginLog)        {            //byte[] bs = Encoding.UTF8.GetBytes(Wsdlxml(loginLog));            byte[] bs = Encoding.UTF8.GetBytes(loginLog);            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://10.109.209.90:21010/gather/services/AioxPort");            myRequest.Method = "POST";            myRequest.ContentType = "text/xml; charset=utf-8";            myRequest.Headers.Add("SOAPAction", "http://tempuri.org/mediate");            myRequest.ContentLength = bs.Length;            //Console.WriteLine("完成准备工作");            using (Stream reqStream = myRequest.GetRequestStream())            {                reqStream.Write(bs, 0, bs.Length);            }            using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())            {                StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);                var rs = sr.ReadToEnd();                return rs;                //Console.WriteLine("反馈结果" + responseString);            }            //Console.WriteLine("完成调用接口");        }        #region 生成审计日志内容        ///         /// 生成日志内容        ///         /// 表格的行        /// 操作类型1=登录 2=模块操作        /// 
public string Generate4ALogXml(DataRow dr, int opType) { string opTypeName = ""; string moduleName = ""; string clientNetAddr = ""; string clientAddr = ""; if (opType == 1)//登录 { opTypeName = "登录"; moduleName = "登录模块"; clientNetAddr = dr["CLIENT_NETWORK_ADDRESS"].ToString(); clientAddr = dr["CLIENT_ADDRESS"].ToString(); } else if (opType == 2)//模块操作 { opTypeName = "模块操作"; moduleName = dr["PERMISSION"].ToString(); clientNetAddr = dr["CLIENT_NETWORK_ADDRESS"].ToString(); clientAddr = dr["CLIENT_ADDRESS"].ToString(); } string xml = ""; string itemXml = string.Format(@"
4ABOSSLog
1
SCNGJJXC
{0}
{1}
{2}
1-KZYYT-10001
{6}
1
{3}
0
login
{7}
{4}
{5}
10.95.240.6
", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), dr["SUB_ACCOUNT_NAME"].ToString(), dr["OPERATE_TIME"].ToString().Replace("/", "-"), dr["OPERATE_CONTENT"].ToString(), clientNetAddr, clientAddr, opTypeName, moduleName); xml = string.Format(@"
{0}
", itemXml); return xml; } #endregion #region 查询满足审计的日志sql /// /// 获取满足条件的登录日志 /// ///
public DataTable GetLoginLog(DateTime currTime) { string sql = string.Format(@"SELECT '' OPERATE_CONTENT,A.ID, A.LOGIN_TIME OPERATE_TIME, B.LOGIN_NAME SUB_ACCOUNT_NAME, A.LOGIN_HOST CLIENT_NETWORK_ADDRESS, A.LOGIN_HOST CLIENT_ADDRESS FROM CC_LOGIN_HISTORY A, CC_USER B,JJXCNO_FOR_4A C WHERE A.USER_ID = B.ID AND A.LOGIN_TIME > TIMESTAMP('2017-6-1 0:00:00') AND A.TAG IS NULL AND C.NO_JJXC=B.LOGIN_NAME"); return ExcuteToTable(sql); } /// /// 获取满足条件的模块操作日志 /// ///
public DataTable GetMoudelLog(DateTime currTime) { string sql = string.Format(@" SELECT A.*, CLIENT_NETWORK_ADDRESS, CLIENT_ADDRESS FROM (SELECT A.ID, A.LOGIN_NAME, A.LOGIN_NAME SUB_ACCOUNT_NAME, A.PERMISSION, A.LOG_DATE OPERATE_TIME, '' OPERATE_CONTENT FROM CC_OPERATION_LOG A, JJXCNO_FOR_4A B WHERE A.TAG IS NULL AND A.LOG_DATE > TIMESTAMP ('2017-6-1 0:00:00') AND B.NO_JJXC = A.LOGIN_NAME) A LEFT JOIN (SELECT * FROM (SELECT BB.*, ROW_NUMBER () OVER (PARTITION BY BB.LOGIN_TIME, BB.LOGIN_NAME) AS NUM FROM (SELECT TO_CHAR (LOGIN_TIME, 'YYYY-MM-DD') LOGIN_TIME, B.LOGIN_NAME, AA.LOGIN_HOST CLIENT_NETWORK_ADDRESS, AA.LOGIN_HOST CLIENT_ADDRESS FROM CC_LOGIN_HISTORY AA, CC_USER B WHERE AA.USER_ID = B.ID AND AA.LOGIN_TIME > TIMESTAMP ('2017-6-1 0:00:00')) BB) WHERE NUM = 1) B ON ( TO_CHAR (A.OPERATE_TIME, 'YYYY-MM-DD') = B.LOGIN_TIME AND A.LOGIN_NAME = B.LOGIN_NAME)"); return ExcuteToTable(sql); } #endregion #region 数据库操作 public DataTable ExcuteToTable(string sql) { //SqlConnection myConnection = new SqlConnection("server=(local);Integrated Security=SSPI;database=northwind"); string sqlConn = "Database=jjxc;UID=jjxc_jk;PWD=bGWN=2R@"; DataSet ds = Db2Helper.Helper_DB2.ExecuteDataset(sqlConn, System.Data.CommandType.Text, sql); return ds.Tables[0]; } public int ExecuteNonQuery(string sql) { string sqlConn = "Database=jjxc;UID=jjxc_jk;PWD=bGWN=2R@"; return Db2Helper.Helper_DB2.ExecuteNonQuery(sqlConn, System.Data.CommandType.Text, sql); } #endregion }}
View Code

参考网址:

转载于:https://www.cnblogs.com/senyier/p/6783555.html

你可能感兴趣的文章
LYNC2013部署系列PART3:前端部署
查看>>
Apache XML-RPC Client Classes
查看>>
shell if [ -f .... ]
查看>>
djagon实战form数据库等操作
查看>>
ISIS的高级属性
查看>>
How To系列(二):how to baidu dork
查看>>
Nginx %00空字节执行任意代码(php)漏洞
查看>>
WordPress主题目录结构说明
查看>>
(总结)Nginx使用的php-fpm的两种进程管理方式及优化
查看>>
启动APACHE出现“error while loading shared libraries: libiconv.so.2”
查看>>
MFS文件系统安装指南
查看>>
查询dsjob
查看>>
Oracle与SQL Server互连
查看>>
JavaScript颜色选择器插件
查看>>
区块链和电子商务 | 大规模电子购物
查看>>
深入理解Fsync
查看>>
c++构造函数详解
查看>>
定制 LAMP 网站服务平台
查看>>
shell中数字计算方法(bc/expr/$(())/let/awk)
查看>>
关于CDH6的一些介绍
查看>>