Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
InternetReadFile() Returns Blank String
Message
De
20/01/2007 16:12:01
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Web Services
Titre:
InternetReadFile() Returns Blank String
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Divers
Thread ID:
01187397
Message ID:
01187397
Vues:
229
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;
        }
    }
}
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform