C#开发ISAPI程序(摘)

C#开发ISAPI程序(摘)

 

我的注释:不知道这么做出来的dll文件,能否应用在非.Net项目中

 

 

主要的步骤
1)编写DLL,处理特定的HTTP请求
2)在web项目里引用刚编写的DLL,并在web.config里注册
3)完成以上两项已经可以在VS2005,2008里测试,但如果要部署到服务器还需要进行IIS的配置
    a)在VS2005,2008里发布网站,2008里可以发布到一个新的文件地址
    b)在新的文件地址上建立一个网站
    c)网站属性 打开主目录选项卡 点配置 在映射选项卡里添加新的应用程序扩展,并指定可执行文件为
       c:/windows/microsoft.net/framework/v2.0.50727/aspnet_isapi.dll(并非是我们自己编写的DLL)
 
最后需要指出的是,WINXP环境只能完成1)2)步而无法完成3),如果需要部署就必须在windows server 2003或其他服务器版本之上,另外MSDN上说部署ISAPI及其web程序对IIS版本也有要求,似乎最低是IIS6.0,那就是说windows server 2000能不能部署需要打一个问号的,2000的IIS似乎是5.1版本,公司没有使用2000的服务器所有没有办法测试,另外7.0的部署似乎也有别6.0的部署,具体看MSDN吧,在MSDN里搜索IHttpHandler 或者直接输入
ms-help://MS.MSDNQTR.v90.chs/dv_vwdcon/html/f5d7bdde-f52d-4f5e-8f86-397378ed1024.htm

--------------------------------------------------------------------------------

/*
CustomHandler.DLL
此DLL处理JPG文件的HTTP请求
需要在项目引用里添加System.Web 这个DLL文件
*/
using System;
using System.Web;
using System.IO;
namespace CustomHandler
{
    public class JpgHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            // 获取文件服务器端物理路径
            string FileName = context.Server.MapPath(context.Request.FilePath);
            context.Response.ContentType = "image/JPEG";
            context.Response.WriteFile(FileName);
            //记录请求JPG文件的浏览者IP
            string s = context.Request.ServerVariables["Remote_Addr"].ToString();
            WriteToLog(DateTime.Now.ToString() + ":" + s + "/r/n");
        }
        public bool IsReusable
        {
            get { return true; }
        }
        public void WriteToLog(string strLog)
        {
            byte[] bts = System.Text.Encoding.Default.GetBytes(strLog);
            string sPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "App.Log";
            FileStream File = new FileStream(sPath, System.IO.FileMode.Append);
            int Length = (int)File.Length;
            File.Lock(0, Length);
            File.Write(bts, 0, (int)bts.Length);
            File.Unlock(0, Length);
            File.Close();
        }
    }
}

--------------------------------------------------------------------------------
在项目里引用CustomerHandler.DLL 并在web.config注册
<configuration>
  <system.web>
    <httpHandlers>
        <add verb="*" path="*.jpg" type="CustomHandler.JpgHandler, CustomHandler"/>
    </httpHandlers>
  <system.web>
</configuration>
一般config文件里已经包含httphandler节点,直接把add行插入进去即可,type格式形如 名字空间.类名,DLL名
无论是从IE直接请求web项目的JPG文件或是从页面包含,在发送文件的同时我们都会记录下用户的IP地址,生成的log文件在网站的根目录下,当然也可以写入数据库

--------------------------------------------------------------------------------

部署详见开头的叙述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
获取海康ISAPI布防类的C#代码如下: ```csharp using System; using System.IO; using System.Net; using System.Text; using System.Xml; using System.Security.Cryptography.X509Certificates; using System.Net.Security; namespace HikvisionISAPI { public class HikvisionISAPI { private string _ip; private string _port; private string _username; private string _password; private string _sessionID; //构造函数 public HikvisionISAPI(string ip, string port, string username, string password) { _ip = ip; _port = port; _username = username; _password = password; _sessionID = string.Empty; } //获取登录会话ID private void GetSessionID() { string url = string.Format("https://{0}:{1}/ISAPI/Security/sessionLogin", _ip, _port); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; string postData = string.Format("username={0}&password={1}", _username, _password); byte[] data = Encoding.UTF8.GetBytes(postData); request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(response.GetResponseStream()); XmlNode node = xmlDoc.SelectSingleNode("//SessionID"); if (node != null) { _sessionID = node.InnerText; } } //获取布防状态 public string GetAlarmStatus(string channelID) { if (string.IsNullOrEmpty(_sessionID)) { GetSessionID(); } string url = string.Format("https://{0}:{1}/ISAPI/Event/notification/alertStream/{2}", _ip, _port, channelID); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "application/xml"; request.Headers.Add("Authorization", "Session " + _sessionID); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string responseContent = string.Empty; using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream, Encoding.UTF8); responseContent = reader.ReadToEnd(); } return responseContent; } //检查证书 private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; } } } ``` 使用方法: ```csharp //创建 HikvisionISAPI 实例 HikvisionISAPI hik = new HikvisionISAPI("192.168.1.100", "80", "admin", "12345"); //获取通道ID为1的布防状态 string alarmStatus = hik.GetAlarmStatus("1"); Console.WriteLine(alarmStatus); ``` 需要注意的是,在调用GetSessionID方法时,需要根据实际情况修改ISAPI的登录接口地址。同时,在请求时需要添加Authorization头部,其值为Session+空格+SessionID。另外,在使用GetAlarmStatus方法获取布防状态时,需要将通道ID作为参数传入。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值