-1

The code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace Battlefield_Servers_Ping
{
    public partial class Form1 : Form
    {
        private string appDirectory;
        private string htmlFilesDirectory;
        private string localFilename;
        private string mainurl;
        private string[] serversip;

        public Form1()
        {
            InitializeComponent();

            appDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath);
            htmlFilesDirectory = "HtmlFilesDirectory";
            htmlFilesDirectory = Path.Combine(appDirectory, htmlFilesDirectory);

            Directory.CreateDirectory(htmlFilesDirectory + "\\Tests");
            mainurl = "http://www.game-monitor.com/search.php?game=bfbc2&&num=";
            using (WebClient client = new WebClient())
            {
                for (int i = 10; i <= 100; i += 10)
                {
                    client.DownloadFile(mainurl + i, htmlFilesDirectory + "\\Tests\\" + i + ".html");
                }
            }

        }
    }
}

In the end i have on the hard disk 10 html files each one is 1kb and inside i see:

[ Access Denied #2 ]

It's not a problem with saving the files they are on the hard disk in the directory i wanted. Could a problem with the site ?

The variable htmlFilesDirectory is:

C:\\Users\\bout0_000\\AppData\\Local\\Tests\\Tests\\HtmlFilesDirectory

The webpage source is:

http://www.game-monitor.com/search.php?game=bfbc2&&num=10

If im in the broswer chrome in this page on empty place make save as i can save the html file no problems. And also edit it later. When im doing save as the name of the file is:

Battlefield Bad Company 2 Servers   Server   Player Search   Game-Monitor.com    Server Search, Monitoring, Stats and more

But in my program im saving it diffrenet. The address is different.

user3200169
  • 275
  • 4
  • 17

2 Answers2

0

Ah I see. You can't save a file like that from another domain.

What you need to do is request the HTML into a stream and save it to file.

Try using WebRequest: http://msdn.microsoft.com/en-us/library/system.net.webrequest(v=vs.110).aspx

Papa
  • 1,628
  • 1
  • 11
  • 16
  • It's not working. Maybe you can try my code and see it. The content of the downloaded html file is [ Access Denied #2 ]. – user3200169 Feb 27 '14 at 00:24
0

It seems that the website is blocking request except from browser. To confirm, you can try to use DownloadString method, that will return content of the web page as string :

var result = client.DownloadString(mainurl + i);

Put breakpoint there, and you'll see "[ Access Denied #2 ]" instead of html content like seen in browser.

There is no instant workaround for this limitation. You may want to try to follow this post :

How can I emulate a web browser http request from code?

to emulate request that sent to the website from your web browser.

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137