C# XMLHTTP 读文件

来源:互联网 发布:cf手游刷枪软件不封号 编辑:程序博客网 时间:2024/06/05 16:40

 

private void Form1_Load(object sender, EventArgs e)        {            var _bytes = HttpReadFile("csdn.net");            Debug.WriteLine(Encoding.UTF8.GetString(_bytes));            MessageBox.Show(_bytes.Length.ToString());        }        [DllImportAttribute("wininet.dll")]        [return: MarshalAs(UnmanagedType.Bool)]        static extern bool InternetGetConnectedState(                int lpdwFlags,                int dwReserved            );        private byte[] HttpReadFile(string URL, bool GET = true)        {            var _ComType = Type.GetTypeFromProgID("Microsoft.XMLHTTP", true);            if (_ComType == null) { return null; }            var _ComObject = Activator.CreateInstance(_ComType, true);            if (_ComObject == null) { return null; }            if (!InternetGetConnectedState(0, 0)) { return null; }            _ComType.InvokeMember("Open", BindingFlags.InvokeMethod, null, _ComObject, new object[] { GET ? "GET" : "POST", URL, false });            _ComType.InvokeMember("Send", BindingFlags.InvokeMethod, null, _ComObject, null);            var _bytes = (byte[])_ComType.InvokeMember("ResponseBody", BindingFlags.GetProperty, null, _ComObject, null);            Marshal.ReleaseComObject(_ComObject);            return _bytes;        }


 

using System.Reflection;using System.Diagnostics;using System.Runtime.InteropServices;


 

本来想用API写的不过最后还是算了,API看着不是多么滴爽

// 至少用下列4个API

InternetOpen 打开Internet

InternetOpenUrl 打开URL

InternetReadFile 读Internet文件

InternetClosaHandle 关闭Internet对象

// 恍然一看,就觉得不高端了

 

0 0
原创粉丝点击