using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace BLAWBLAW { public partial class FormMain : Form { [DllImport("wininet.dll")] private static extern int InternetOpen(string sAgent, int lAccessType, string sProxyName, string sProxyBypass, int lFlags); [DllImport("wininet.dll")] private static extern int InternetOpenUrl(int hInternetSession, string sUrl, string sHeaders, int lHeadersLength, uint lFlags, int lContext); [DllImport("wininet.dll")] private static extern int InternetReadFile(int hfile, ref string sBuffer, int lNumberofBytesToRead, ref int lBytesRead); [DllImport("wininet.dll")] private static extern short InternetCloseHandle(int hInst); public FormMain() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { const int INTERNET_OPEN_TYPE_PRECONFIG = 0; const int SYNCHRONOUS = 0; const uint INTERNET_FLAG_RELOAD = 2147483648; string UrlName = "http://www.cnn.com"; string lc_Contents; string sAgent = "VS .net 2005"; // what application is using Internet services? int hInternetSession; int hUrlFile; int isOK = -1; int lBytesRead = -1; string sReadBuffer; int loop1 = 1; hInternetSession = InternetOpen(sAgent, INTERNET_OPEN_TYPE_PRECONFIG, "", "", SYNCHRONOUS); if (hInternetSession == 0) { MessageBox.Show("Internet session cannot be established"); return; } hUrlFile = InternetOpenUrl(hInternetSession, UrlName, "", 0, INTERNET_FLAG_RELOAD, 0); if (hUrlFile == 0) { MessageBox.Show("URL cannot be opened"); return; } sReadBuffer = ""; lc_Contents = ""; while(isOK != 0 && lBytesRead != 0) { // set aside a wicked huge buffer for (loop1 = 1; loop1 <= 32767; loop1++) sReadBuffer += " "; // Config lBytesRead = -1; isOK = InternetReadFile(hUrlFile, ref sReadBuffer, sReadBuffer.Length, ref lBytesRead); lc_Contents += sReadBuffer; if (isOK == 0 || lBytesRead == 0) break; } // close all the handles we opened InternetCloseHandle(hUrlFile); InternetCloseHandle(hInternetSession); richTextBox1.Text = lc_Contents; } } }