C# NetCut / NetworkCut

来源:互联网 发布:装修平面图设计软件 编辑:程序博客网 时间:2024/06/04 18:08

本类库实际上是一种很失败的东西,至少在我看来是足够失败 当时也不知道是头发热

还是人未醒,居然会去拦截WinInet,看来几年前学习的WebProxy与HTTP是白学了,

我想应该是 Retrieve-HttpOnly-Session-Cookie-in-WebBrowser 这篇帖子中的一些话

误导了我把,本类库的源代码实际上只是提供给大家学习API-HOOK的一个小小应用

不过API-HOOK分了好几种,我最喜欢的还是IAT,EAT,INLINE三种,不过论到实

用性INLINE是最好的,RING3级 稳定性与安全性还是很OK的、

Hooking the methods exposed by WININET.DLL gives the ability to interact with each request sending to server, including the AJAX request! 

The probable call sequence is shown as below: 

1=> InternetOpen2=> InternetConnect3=> HttpOpenRequest// .............X=> InternetCloseHandle  
By hooking these methods, it is possible for us to detect each request sent to the web server, and inject our codes before / after each request.

Why not WINSOCK? It is easier to hook methods exposed by WININET.DLL than hooking WINSOCK methods, since the former wraps the HTTP protocol.

There are two different ways in hooking, inline hook and routine hook. Inline hook is universal for almost all of the cases, however, routine hook is more robust. The sample implements it by modifying the IAT(Import Address Table), one of the routine hooks.

If you need more details about IAT hook, here is an article for reference. 

好吧,不在给自己找理由,本类库已经修正了一些命名 主要是把源代码移植到使用 API-Hook / RING3

上,我注解一些命名上的变动,实际上你改回去也是一样的。

Inline-Hook ->Ex-> NetHook

NetCut -> NetworkCut

NetTab -> NetworkTable

NetworkProtocol -> NetServer

NetTabCollection -> NetworkTableCollection

NetCutRequestComplete -> NetworkCutRequestComplete


不过我们到也可以可以看看,如果通过“封包 截包”技术可以轻松得到什么?


该处拦截的是ws2_32.dll导出的函数,有些人会问为什么不去拦截wsock32.dl呢?

你可以这样去看待,实际上这两个DLL并没有太大的区别,一个微软用于CString

一个用于string,本质上它是 CString -> string 所以是 wsock32.dll -> ws2_32.dll

Http请求首先会去解析对方主机的地址表,然后选择一个候选地址通过socket连

接对方的主机的80端口,一个完整的Http请求首先是客户端发送协议头,中包含

请求的协议头,对方接收到后进行一个处理并进行相应我们只需要去接受来自服

务器返回的数据包即可,微软在Internet Explorer 9中支持开发人员可以网络抓包

大概则是这样去实现的, 我是不太相信微软开发人员可能会使用 SOCK SPI 的、

在NetCut中你也可以实现获取类似的功能,只是相对性的会复杂一些,那么我举

一个列子我们需要获取请求返回给客户端的Cookie,如何去做呢?

[csharp] view plaincopy
  1. // example:  
  2. private const int HTTP_QUERY_SET_COOKIE = 43;  
  3.   
  4. private void _NetCut_RequestComplete(object sender, NetCut.NetTab e)  
  5. {  
  6. StringBuilder strBufferBuider = new StringBuilder(4096);  
  7. string strSetCookieValue = e.QueryInfo(e.RequestHandle, HTTP_QUERY_SET_COOKIE, strBufferBuider);  
  8. if(strSetCookieValue != null && strSetCookieValue.Length > 0)  
  9. Console.WriteLine(strSetCookieValue);  
  10. }  
上面则是通过NetTab去获取请求返回给客户端的Cookie,如果有则返回否则会返回null

不过在NetTab对象中可以通过ResponseCookie属性获取Cookie,你可以不必使用上述

代码进行获取服务器返回给客户端的Cookie


好的,如果我们需要客户端发送请求附加的Cookie需要调用到WININET.DLL中导出的

两个函数 InternetGetCookie / InternetGetCookieEx 便可以了、

C# Declare:

[csharp] view plaincopy
  1. [DllImport("wininet.dll", SetLastError=true)]  
  2. public static extern bool InternetGetCookie(  
  3.   string url, string cookieName, StringBuilder cookieData, ref int size);  
[csharp] view plaincopy
  1. [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]  
  2. static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);  
如果你需要查阅NetCut中的源代码,那么你首先需要下载:CAT / Inline-Hook.dll

如果你需要阅读一些相关的资料可以查阅C#方面的内容: Inline-Hook / CSDN Blog.

当你下载完毕后解压 会看其中有包含4.0、2.0的两个文件夹根据你的项目

中使用的.NET框架版本打开对应版本的文件夹

注意一般项目默认是AnyCPU,在64位系统中默认为x64,在32系统中默认为x86

所以你在选择DLL的时候一定要注意这样的问题,在代码中会使用到不安全代码

Unsafe,也可以说是本地代码 所以你必须要在项目属性中勾选“允许不安全代码”


在上面我是使用的x86程序所以,我需要X86的DLL且因我的项目.NET版本为4.0


现在我们需要添加项目引用,点击“添加引用(R)”


在弹出的“引用管理中”点击“浏览(B)”按钮,然后我们把需要引用的DLL路径复制进去

点击“选择要引用的文件...”窗口中“添加”按钮,OK、


需要注意的地方基本上没有了现在就是贴代码、首先是NetHook部分

[csharp] view plaincopy
  1. namespace System.Runtime.InteropServices  
  2. {  
  3.     public class NetHook : InlineHook  
  4.     {  
  5.         public void Install(string strLibraryName, string strMethodName, IntPtr newMethodAddress)  
  6.         {  
  7.             IntPtr oldMethodAddress = base.GetProcAddress(strLibraryName, strMethodName);  
  8.             base.Install(oldMethodAddress, newMethodAddress);  
  9.         }  
  10.     }  
  11. }  
在上面为了支持源NetCut类的源代码,所以我必须在此处为InlineHook扩展一个Install方法

NetCut使用的办法,你可以参阅 NetCut / CSDN Blog. 包含示例代码 两个类是相等的、

下面是NetCut类的源代码,有一些不足之处已经被我小小修复了一下、本类工作最多的部分

是在NetworkTableCollection,其他类实在是没做什么、不过需要你去了解需求使用的 API、

[csharp] view plaincopy
  1. using System.Text;  
  2. using System.Collections.Generic;  
  3. using System.Runtime.InteropServices;  
  4.  
  5. #pragma warning disable 649  
  6.   
  7. namespace System.Net  
  8. {  
  9.     [Flags]  
  10.     public enum NetworkProtocol : uint  
  11.     {  
  12.         Ftp = 1,  
  13.         Gopher,  
  14.         Http  
  15.     }  
  16.   
  17.     unsafe class Win32Native  
  18.     {  
  19.         [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]  
  20.         public static extern IntPtr InternetConnect(  
  21.                 IntPtr hInternet,  
  22.                 string lpszServerName,  
  23.                 short nServerPort,  
  24.                 string lpszUsername,  
  25.                 string lpszPassword,  
  26.                 int dwService,  
  27.                 int dwFlags,  
  28.                 IntPtr dwContext  
  29.             );  
  30.   
  31.         [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]  
  32.         public static extern IntPtr HttpOpenRequest(  
  33.                 IntPtr hConnect,  
  34.                 string lpszVerb,  
  35.                 string lpszObjectName,  
  36.                 string lpszVersion,  
  37.                 string lpszReferer,  
  38.                 char** lplpszAcceptTypes,  
  39.                 int dwFlags,  
  40.                 IntPtr dwContext  
  41.             );  
  42.   
  43.         [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]  
  44.         public static extern bool HttpAddRequestHeaders(  
  45.                 IntPtr hRequest,  
  46.                 string pwszHeaders,  
  47.                 int dwHeadersLength,  
  48.                 uint dwModifiers  
  49.             );  
  50.   
  51.         [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]  
  52.         public static extern bool HttpSendRequest(  
  53.                 IntPtr hRequest,  
  54.                 string lpszHeaders,  
  55.                 uint dwHeadersLength,  
  56.                 IntPtr lpOptional,  
  57.                 uint dwOptionalLength  
  58.             );  
  59.   
  60.         [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]  
  61.         public static extern bool HttpQueryInfo(  
  62.                 IntPtr hInternet,  
  63.                 int dwInfoLevel,  
  64.                 StringBuilder lpBuffer,  
  65.                 ref uint lpdwBufferLength,  
  66.                 IntPtr lpdwIndex  
  67.             );  
  68.   
  69.         [DllImport("WinInet.dll", SetLastError = true)]  
  70.         [return: MarshalAs(UnmanagedType.Bool)]  
  71.         public static extern bool InternetCloseHandle(IntPtr hInternet);  
  72.   
  73.         public static readonly IntPtr NULL = IntPtr.Zero;  
  74.         public const int HTTP_QUERY_RAW_HEADERS = 21;  
  75.         public const int HTTP_QUERY_RAW_HEADERS_CRLF = 22;  
  76.         public const int HTTP_QUERY_STATUS_CODE = 19;  
  77.         public const int HTTP_QUERY_SET_COOKIE = 43;  
  78.     }  
  79.   
  80.     public delegate void NetworkCutRequestComplete(object sender, NetworkTable e);  
  81.   
  82.     public partial class NetworkCut  
  83.     {  
  84.         private class NetworkCutBase  
  85.         {  
  86.             public NetHook InternetConnectA;  
  87.             public NetHook InternetConnectW;  
  88.             public NetHook HttpOpenRequestA;  
  89.             public NetHook HttpOpenRequestW;  
  90.             public NetHook HttpAddRequestHeadersA;  
  91.             public NetHook HttpAddRequestHeadersW;  
  92.             public NetHook HttpSendRequestA;  
  93.             public NetHook HttpSendRequestW;  
  94.   
  95.             public NetworkCutBase()  
  96.             {  
  97.                 var fields = typeof(NetworkCutBase).GetFields();  
  98.                 foreach (var field in fields)  
  99.                     field.SetValue(thisnew NetHook());  
  100.             }  
  101.         }  
  102.   
  103.         private unsafe class Win32Pointer  
  104.         {  
  105.             [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]  
  106.             public delegate IntPtr InternetConnectA(  
  107.                      IntPtr hInternet,  
  108.                      string lpszServerName,  
  109.                      short nServerPort,  
  110.                      string lpszUsername,  
  111.                      string lpszPassword,  
  112.                      int dwService,  
  113.                      int dwFlags,  
  114.                      IntPtr dwContext  
  115.                  );  
  116.   
  117.             [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]  
  118.             public delegate IntPtr InternetConnectW(  
  119.                      IntPtr hInternet,  
  120.                      string lpszServerName,  
  121.                      short nServerPort,  
  122.                      string lpszUsername,  
  123.                      string lpszPassword,  
  124.                      int dwService,  
  125.                      int dwFlags,  
  126.                      IntPtr dwContext  
  127.                  );  
  128.   
  129.             [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]  
  130.             public delegate IntPtr HttpOpenRequestA(  
  131.                     IntPtr hConnect,  
  132.                     string lpszVerb,  
  133.                     string lpszObjectName,  
  134.                     string lpszVersion,  
  135.                     string lpszReferer,  
  136.                     char** lplpszAcceptTypes,  
  137.                     int dwFlags,  
  138.                     IntPtr dwContext  
  139.                 );  
  140.   
  141.             [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]  
  142.             public delegate IntPtr HttpOpenRequestW(  
  143.                     IntPtr hConnect,  
  144.                     string lpszVerb,  
  145.                     string lpszObjectName,  
  146.                     string lpszVersion,  
  147.                     string lpszReferer,  
  148.                     char** lplpszAcceptTypes,  
  149.                     int dwFlags,  
  150.                     IntPtr dwContext  
  151.                 );  
  152.   
  153.             [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]  
  154.             public delegate bool HttpAddRequestHeadersA(  
  155.                     IntPtr hRequest,  
  156.                     string pwszHeaders,  
  157.                     int dwHeadersLength,  
  158.                     uint dwModifiers  
  159.                 );  
  160.   
  161.             [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]  
  162.             public delegate bool HttpAddRequestHeadersW(  
  163.                     IntPtr hRequest,  
  164.                     string pwszHeaders,  
  165.                     int dwHeadersLength,  
  166.                     uint dwModifiers  
  167.                 );  
  168.   
  169.             [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]  
  170.             public delegate bool HttpSendRequestA(  
  171.                    IntPtr hRequest,  
  172.                    string lpszHeaders,  
  173.                    uint dwHeadersLength,  
  174.                    IntPtr lpOptional,  
  175.                    uint dwOptionalLength  
  176.                );  
  177.   
  178.             [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]  
  179.             public delegate bool HttpSendRequestW(  
  180.                    IntPtr hRequest,  
  181.                    string lpszHeaders,  
  182.                    uint dwHeadersLength,  
  183.                    IntPtr lpOptional,  
  184.                    uint dwOptionalLength  
  185.                );  
  186.         }  
  187.     }  
  188.   
  189.     public partial class NetworkCut  
  190.     {  
  191.         private NetworkCutBase mNetCutBase;  
  192.         public event NetworkCutRequestComplete RequestComplete;  
  193.   
  194.         public NetworkCut()  
  195.         {  
  196.             this.mNetCutBase = new NetworkCutBase();  
  197.             this.Items = new NetworkTableCollection(this);  
  198.         }  
  199.   
  200.         public void Install()  
  201.         {  
  202.             this.ExecuteCommand(-1);  
  203.         }  
  204.   
  205.         public void Uninstall()  
  206.         {  
  207.             this.ExecuteCommand(0);  
  208.         }  
  209.   
  210.         public void Suspend()  
  211.         {  
  212.             this.ExecuteCommand(2);  
  213.         }  
  214.   
  215.         public void Resume()  
  216.         {  
  217.             this.ExecuteCommand(1);  
  218.         }  
  219.   
  220.         private void ExecuteCommand(int command)  
  221.         {  
  222.             var asm = typeof(NetworkCutBase).Assembly;  
  223.             var fields = typeof(NetworkCutBase).GetFields();  
  224.             foreach (var field in fields)  
  225.             {  
  226.                 NetHook h = (field.GetValue(mNetCutBase) as NetHook);  
  227.                 switch (command)  
  228.                 {  
  229.                     case -1:  
  230.                         Type td = asm.GetType("System.Net.NetworkCut+Win32Pointer+" + field.Name);  
  231.                         Delegate d = Delegate.CreateDelegate(td, this, field.Name);  
  232.                         h.Install("WinInet.dll", field.Name, Marshal.GetFunctionPointerForDelegate(d));  
  233.                         break;  
  234.                     case 0:  
  235.                         h.Uninstall();  
  236.                         break;  
  237.                     case 1:  
  238.                         h.Resume();  
  239.                         break;  
  240.                     case 2:  
  241.                         h.Suspend();  
  242.                         break;  
  243.                 }  
  244.             }  
  245.         }  
  246.   
  247.         public NetworkTableCollection Items  
  248.         {  
  249.             get;  
  250.             private set;  
  251.         }  
  252.   
  253.         internal void OnRequestComplete(NetworkTable tab)  
  254.         {  
  255.             if (this.RequestComplete != null)  
  256.                 this.RequestComplete(this, tab);  
  257.         }  
  258.     }  
  259.   
  260.     public unsafe partial class NetworkCut  
  261.     {  
  262.         private IntPtr InternetConnectA(IntPtr hInternet, string lpszServerName, short nServerPort,  
  263.                 string lpszUsername, string lpszPassword, int dwService, int dwFlags, IntPtr dwContext)  
  264.         {  
  265.             try  
  266.             {  
  267.                 mNetCutBase.InternetConnectA.Suspend();  
  268.                 IntPtr hConnect = Win32Native.InternetConnect(hInternet, lpszServerName, nServerPort,  
  269.                     lpszUsername, lpszPassword, dwService, dwFlags, dwContext);  
  270.                 this.Items.Add(hInternet, hConnect, lpszServerName, lpszUsername, lpszPassword, dwService, nServerPort);  
  271.                 return hConnect;  
  272.             }  
  273.             finally  
  274.             {  
  275.                 mNetCutBase.InternetConnectA.Resume();  
  276.             }  
  277.         }  
  278.   
  279.         private IntPtr InternetConnectW(IntPtr hInternet, string lpszServerName, short nServerPort,  
  280.                 string lpszUsername, string lpszPassword, int dwService, int dwFlags, IntPtr dwContext)  
  281.         {  
  282.             try  
  283.             {  
  284.                 mNetCutBase.InternetConnectW.Suspend();  
  285.                 IntPtr hConnect = Win32Native.InternetConnect(hInternet, lpszServerName, nServerPort,  
  286.                     lpszUsername, lpszPassword, dwService, dwFlags, dwContext);  
  287.                 this.Items.Add(hInternet, hConnect, lpszServerName, lpszUsername, lpszPassword, dwService, nServerPort);  
  288.                 return hConnect;  
  289.             }  
  290.             finally  
  291.             {  
  292.                 mNetCutBase.InternetConnectW.Resume();  
  293.             }  
  294.         }  
  295.   
  296.         private IntPtr HttpOpenRequestA(IntPtr hConnect, string lpszVerb, string lpszObjectName, string lpszVersion,  
  297.                 string lpszReferer, char** lplpszAcceptTypes, int dwFlags, IntPtr dwContext)  
  298.         {  
  299.             try  
  300.             {  
  301.                 mNetCutBase.HttpOpenRequestA.Suspend();  
  302.                 IntPtr hRequest = Win32Native.HttpOpenRequest(hConnect, lpszVerb, lpszObjectName, lpszVersion, lpszReferer, lplpszAcceptTypes, dwFlags, dwContext);  
  303.                 this.Items.Add(hConnect, hRequest, lpszVerb, lpszObjectName, lplpszAcceptTypes, lpszVersion);  
  304.                 return hRequest;  
  305.             }  
  306.             finally  
  307.             {  
  308.                 mNetCutBase.HttpOpenRequestA.Resume();  
  309.             }  
  310.         }  
  311.   
  312.         private IntPtr HttpOpenRequestW(IntPtr hConnect, string lpszVerb, string lpszObjectName, string lpszVersion,  
  313.                 string lpszReferer, char** lplpszAcceptTypes, int dwFlags, IntPtr dwContext)  
  314.         {  
  315.             try  
  316.             {  
  317.                 mNetCutBase.HttpOpenRequestW.Suspend();  
  318.                 IntPtr hRequest = Win32Native.HttpOpenRequest(hConnect, lpszVerb, lpszObjectName, lpszVersion, lpszReferer, lplpszAcceptTypes, dwFlags, dwContext);  
  319.                 this.Items.Add(hConnect, hRequest, lpszVerb, lpszObjectName, lplpszAcceptTypes, lpszVersion);  
  320.                 return hRequest;  
  321.             }  
  322.             finally  
  323.             {  
  324.                 mNetCutBase.HttpOpenRequestW.Resume();  
  325.             }  
  326.         }  
  327.   
  328.         private bool HttpAddRequestHeadersA(IntPtr hRequest, string pwszHeaders, int dwHeadersLength, uint dwModifiers)  
  329.         {  
  330.             try  
  331.             {  
  332.                 this.Items.Add(hRequest, pwszHeaders);  
  333.                 mNetCutBase.HttpAddRequestHeadersA.Suspend();  
  334.                 return Win32Native.HttpAddRequestHeaders(hRequest, pwszHeaders, dwHeadersLength, dwModifiers);  
  335.             }  
  336.             finally  
  337.             {  
  338.                 mNetCutBase.HttpAddRequestHeadersA.Resume();  
  339.             }  
  340.         }  
  341.   
  342.         private bool HttpAddRequestHeadersW(IntPtr hRequest, string pwszHeaders, int dwHeadersLength, uint dwModifiers)  
  343.         {  
  344.             try  
  345.             {  
  346.                 this.Items.Add(hRequest, pwszHeaders);  
  347.                 mNetCutBase.HttpAddRequestHeadersW.Suspend();  
  348.                 return Win32Native.HttpAddRequestHeaders(hRequest, pwszHeaders, dwHeadersLength, dwModifiers);  
  349.             }  
  350.             finally  
  351.             {  
  352.                 mNetCutBase.HttpAddRequestHeadersW.Resume();  
  353.             }  
  354.         }  
  355.   
  356.         private bool HttpSendRequestA(IntPtr hRequest, string lpszHeaders, uint dwHeadersLength, IntPtr lpOptional, uint dwOptionalLength)  
  357.         {  
  358.             try  
  359.             {  
  360.                 mNetCutBase.HttpSendRequestA.Suspend();  
  361.                 return this.Items.Add(hRequest, lpOptional, dwOptionalLength,  
  362.                         Win32Native.HttpSendRequest(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)  
  363.                     );  
  364.             }  
  365.             finally  
  366.             {  
  367.                 mNetCutBase.HttpSendRequestA.Resume();  
  368.             }  
  369.         }  
  370.   
  371.         private bool HttpSendRequestW(IntPtr hRequest, string lpszHeaders, uint dwHeadersLength, IntPtr lpOptional, uint dwOptionalLength)  
  372.         {  
  373.             try  
  374.             {  
  375.                 mNetCutBase.HttpSendRequestW.Suspend();  
  376.                 return this.Items.Add(hRequest, lpOptional, dwOptionalLength,  
  377.                         Win32Native.HttpSendRequest(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength)  
  378.                     );  
  379.             }  
  380.             finally  
  381.             {  
  382.                 mNetCutBase.HttpSendRequestW.Resume();  
  383.             }  
  384.         }  
  385.     }  
  386.   
  387.     public sealed class NetworkTableCollection : List<NetworkTable>  
  388.     {  
  389.         private NetworkCut mNetCut;  
  390.         private StringBuilder mBuffer = new StringBuilder(4096);  
  391.   
  392.         internal NetworkTableCollection(NetworkCut netCut)  
  393.         {  
  394.             this.mNetCut = netCut;  
  395.         }  
  396.   
  397.         internal bool Add(IntPtr RequestHandle, IntPtr Data, uint Length, bool SendRequest)  
  398.         {  
  399.             if (RequestHandle != Win32Native.NULL)  
  400.                 for (int i = 0, nNoOfStatusCode; i < base.Count; i++)  
  401.                 {  
  402.                     var item = base[i];  
  403.                     if (item.RequestHandle == RequestHandle)  
  404.                     {  
  405.                         if (SendRequest)  
  406.                         {  
  407.                             item.SendRequest = SendRequest;  
  408.                             item.ResponseStatus = NetworkTable.HttpQueryInfo(RequestHandle, Win32Native.HTTP_QUERY_RAW_HEADERS, mBuffer);  
  409.                             item.ResponseCookie = NetworkTable.HttpQueryInfo(RequestHandle, Win32Native.HTTP_QUERY_SET_COOKIE, mBuffer);  
  410.                             int.TryParse(NetworkTable.HttpQueryInfo(RequestHandle, Win32Native.HTTP_QUERY_STATUS_CODE, mBuffer), out nNoOfStatusCode);  
  411.                             this.Add(item.ResponseHeaders, NetworkTable.HttpQueryInfo(RequestHandle, Win32Native.HTTP_QUERY_RAW_HEADERS_CRLF, mBuffer));  
  412.                             item.StatusCode = (HttpStatusCode)nNoOfStatusCode;  
  413.                         }  
  414.                         if (Data != Win32Native.NULL && Length > 0)  
  415.                         {  
  416.                             item.RequestData = new byte[Length];  
  417.                             Marshal.Copy(Data, item.RequestData, 0, (int)Length);  
  418.                         }  
  419.                         this.mNetCut.OnRequestComplete(item);  
  420.                         break;  
  421.                     }  
  422.                 }  
  423.             return SendRequest;  
  424.         }  
  425.   
  426.         internal void Add(IntPtr RequestHandle, string Headers)  
  427.         {  
  428.             if (RequestHandle != Win32Native.NULL && Headers != null && Headers.Length > 0)  
  429.                 try  
  430.                 {  
  431.                     foreach (var item in this)  
  432.                         if (item.RequestHandle == RequestHandle)  
  433.                             this.Add(item.RequestHeaders, Headers);  
  434.                 }  
  435.                 catch  
  436.                 {  
  437.                     return;  
  438.                 }  
  439.         }  
  440.   
  441.         internal void Add(WebHeaderCollection Item, string Headers)  
  442.         {  
  443.             if (Headers != null && Headers.Length > 0)  
  444.             {  
  445.                 var splits = Headers.Split('\r''\n');  
  446.                 foreach (var split in splits)  
  447.                 {  
  448.                     int index = -1;  
  449.                     if (split.Length > 0 && (index = split.IndexOf(':')) > -1)  
  450.                     {  
  451.                         var key = split.Substring(0, index++);  
  452.                         var value = split.Substring(++index);  
  453.                         Item.Add(key, value);  
  454.                     }  
  455.                 }  
  456.             }  
  457.         }  
  458.   
  459.         internal void Add(IntPtr SessionHandle, IntPtr ConnectHandle, string Host, string Username, string Password, int Service, short Port)  
  460.         {  
  461.             var item = new NetworkTable();  
  462.             item.RequestPort = Port;  
  463.             item.RequestHost = Host;  
  464.             item.Username = Username;  
  465.             item.Password = Password;  
  466.             item.ConnectHandle = ConnectHandle;  
  467.             item.SessionHandle = SessionHandle;  
  468.             item.ConnectProtocol = (NetworkProtocol)Service;  
  469.             base.Add(item);  
  470.         }  
  471.   
  472.         internal unsafe void Add(IntPtr ConnectHandle, IntPtr RequestHandle, string Method, string Url, char** Accept, string Version)  
  473.         {  
  474.             if (ConnectHandle != Win32Native.NULL)  
  475.                 try  
  476.                 {  
  477.                     foreach (var item in this)  
  478.                         if (item.ConnectHandle == ConnectHandle)  
  479.                         {  
  480.                             item.RequestUrl = Url;  
  481.                             item.RequestMethod = Method;  
  482.                             item.ProtocolVersion = Version ?? "HTTP/1.1";  
  483.                             item.RequestHandle = RequestHandle;  
  484.                             var accept = string.Empty;  
  485.                             while (*Accept != null)  
  486.                                 accept += new string(*Accept++);  
  487.                             item.Accept = accept;  
  488.                             return;  
  489.                         }  
  490.                 }  
  491.                 catch  
  492.                 {  
  493.                     return;  
  494.                 }  
  495.         }  
  496.     }  
  497.   
  498.     public partial class NetworkTable  
  499.     {  
  500.         public NetworkTable()  
  501.         {  
  502.             this.RequestHeaders = new WebHeaderCollection();  
  503.             this.ResponseHeaders = new WebHeaderCollection();  
  504.         }  
  505.   
  506.         public IntPtr ConnectHandle  
  507.         {  
  508.             get;  
  509.             set;  
  510.         }  
  511.   
  512.         public IntPtr RequestHandle  
  513.         {  
  514.             get;  
  515.             set;  
  516.         }  
  517.   
  518.         public IntPtr SessionHandle  
  519.         {  
  520.             get;  
  521.             set;  
  522.         }  
  523.   
  524.         public string RequestMethod // verb  
  525.         {  
  526.             get;  
  527.             set;  
  528.         }  
  529.   
  530.         public string Username  
  531.         {  
  532.             get;  
  533.             set;  
  534.         }  
  535.   
  536.         public string Password  
  537.         {  
  538.             get;  
  539.             set;  
  540.         }  
  541.   
  542.         public short RequestPort  
  543.         {  
  544.             get;  
  545.             set;  
  546.         }  
  547.   
  548.         public string RequestHost  
  549.         {  
  550.             get;  
  551.             set;  
  552.         }  
  553.   
  554.         public string RequestUrl  
  555.         {  
  556.             get;  
  557.             set;  
  558.         }  
  559.   
  560.         public NetworkProtocol ConnectProtocol  
  561.         {  
  562.             get;  
  563.             set;  
  564.         }  
  565.   
  566.         public string Accept  
  567.         {  
  568.             get;  
  569.             set;  
  570.         }  
  571.   
  572.         public string ProtocolVersion  
  573.         {  
  574.             get;  
  575.             set;  
  576.         }  
  577.   
  578.         public WebHeaderCollection RequestHeaders  
  579.         {  
  580.             get;  
  581.             set;  
  582.         }  
  583.   
  584.         public byte[] RequestData  
  585.         {  
  586.             get;  
  587.             set;  
  588.         }  
  589.   
  590.         public WebHeaderCollection ResponseHeaders  
  591.         {  
  592.             get;  
  593.             set;  
  594.         }  
  595.   
  596.         public string ResponseCookie  
  597.         {  
  598.             get;  
  599.             set;  
  600.         }  
  601.   
  602.         public string ResponseStatus  
  603.         {  
  604.             get;  
  605.             set;  
  606.         }  
  607.   
  608.         public HttpStatusCode StatusCode  
  609.         {  
  610.             get;  
  611.             set;  
  612.         }  
  613.   
  614.         public bool SendRequest  
  615.         {  
  616.             get;  
  617.             set;  
  618.         }  
  619.   
  620.         public bool CloseHandle()  
  621.         {  
  622.             return Win32Native.InternetCloseHandle(this.RequestHandle) |  
  623.              Win32Native.InternetCloseHandle(this.ConnectHandle) |  
  624.              Win32Native.InternetCloseHandle(this.SessionHandle);  
  625.         }  
  626.   
  627.         public string QueryInfo(IntPtr Handle, int Query, StringBuilder Buffer)  
  628.         {  
  629.             return HttpQueryInfo(Handle, Query, Buffer);  
  630.         }  
  631.   
  632.         public static string HttpQueryInfo(IntPtr Handle, int Query, StringBuilder Buffer)  
  633.         {  
  634.             uint size = (uint)Buffer.Capacity;  
  635.             if (Win32Native.HttpQueryInfo(Handle, Query, Buffer, ref size, IntPtr.Zero))  
  636.                 return Convert.ToString(Buffer);  
  637.             return null;  
  638.         }  
  639.     }  
  640. }  
转载自:http://blog.csdn.net/u012395622/article/details/46943193
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 鼻子通向嘴那里痒得难受怎么办 小孩上嘴唇中间的连线碰掉了怎么办 秋田犬夏天退毛严重么 怎么办 初中数学基本没学过高中怎么办 老师家纺突然想日语文老师怎么办 微信聊天表情小企鹅不动了怎么办 微信自带小表情不全怎么办 爱奇艺电视果有图像无声音怎么办 微信表情包保存不到手机相册怎么办 才出生的兔宝宝被母兔抓伤了怎么办 老婆生气了说恨我一辈子我该怎么办 华为手机微信表情不显示含义怎么办 地下城游戏登录链接一直失败怎么办 聊天时别人打听家人不想回答怎么办 微信钱包零钱密码忘了怎么办 斗图我能怎么办我也很无奈 微信解冻设备不一致申诉失败怎么办 看不懂微信脸部表情什么意思怎么办 有的动图图片过大微信发不了怎么办 微信漂流瓶扔瓶子没有人回复怎么办 删了微信 手贱 添加 怎么办 姨妈弄到床垫上拆不下来洗怎么办 碰到情商智商都高的小人怎么办 微信聊天界面右上角的小人头怎么办 最近摸高摸到的高度越来越矮怎么办 每次孕检显示小孩子体型大怎么办? 阴阳师纸片人蓝色锦囊点掉了怎么办 抱孩子把腰闪了动不了在家怎么办 餐厅客人中有儿童服务时怎么办 脊柱胸段向右侧凸要怎么办 玩球球大作战不小心开自由了怎么办 小孩哭脸后喝水呛着了怎么办 摔跤引起的脸部半边儿僵硬怎么办 老人受了刺激大笑不止是怎么办 想让父母陪着玩 没时间怎么办 开过光的百家锁东西别人碰了怎么办 兔兔助手描述文件变了存档怎么办 扣扣没有绑手机被盗了怎么办 我的扣扣被盗了好友也被删了怎么办 小孩不胖但脖子黑怎么办呢 小孩喜欢歪头斜眼看东西怎么办