Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
InternetReadFile() Returns Blank String
Message
 
To
20/01/2007 16:12:01
General information
Forum:
ASP.NET
Category:
Web Services
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01187397
Message ID:
01187494
Views:
15
There is a much easier way to do this using .Net, you can use the HttpWebRequest class. See an article I have written on this subject at http://www.emoreau.com/Entries/Articles/2004/03/Using-The-WebRequest-object-to-retrieve-Yahoo-stock-quotes.aspx


>I was wondering if any of you can help me on a puzzling bug. I'm writing a C# program which can read text from a web page. The InternetOpen() and InternetOpenUrl() appeared to work fine. The problem is the InternetReadFile() always return lBytesRead = 0 and sReadBuffer is blank. Below is the complete code. The funny part is the equivalent code works perfectly fine in Foxpro.
>
>
>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;
>        }
>    }
>}
>
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform