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对象
// 恍然一看,就觉得不高端了