Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7937793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:36:52+00:00 2026-06-03T22:36:52+00:00

I’m having issues using StreamWriter to code a scraper for a current project i’ve

  • 0

I’m having issues using StreamWriter to code a scraper for a current project i’ve got. The loop i’ve coded is below

I’ve debugged all the variables coming into the loop and everything is set as it should be. When i pass in a url and the range to search through based on an ID GET variable in the url it fails to write the second sourceCode string

Could someone be kind enough to tell me if i’m not flushing something or is there something else at work here??

I’ve wrecked my head trying to find the root cause but its proving very stubborn

using System;
using System.IO;
using System.Windows.Forms;

namespace Scraper
{
    public partial class Form1 : Form
    {
        Scraper scraper = new Scraper();
        private StreamWriter sw;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = textBox1.Text;
            string[] urlBits = url.Split('.');
            string[] domain = urlBits[2].Split('/');

            string filepath = @"C:\Users\Herbaldinho\Desktop\"+urlBits[1]+"-"+domain[0];
            string parentPath = @"C:\Users\Herbaldinho\Desktop\";
            string newPath = Path.Combine(parentPath, filepath);

            if (File.Exists(filepath))
            {}
            else
            {
                Directory.CreateDirectory(newPath);
            }
            DateTime today = DateTime.Today;
            string curDate = String.Format("{0:ddd-MMM-dd-yyyy}", today);
            string subPath = newPath + "\\" + curDate;
            string newSubPath = Path.Combine(newPath, subPath);

            if (File.Exists(subPath))
            { }
            else
            {
                Directory.CreateDirectory(newSubPath);
            }

            string lower = textBox2.Text;
            int lowerValue;
            int.TryParse(lower, out lowerValue);

            string upper = textBox3.Text;
            int upperValue;
            int.TryParse(upper, out upperValue);

            int i;
            for (i = lowerValue; i < upperValue; i++)
            {
                string filename = newSubPath+"\\Advert-"+i+".html";
                string adPage = url + i;
                bool write = scraper.UrlExists(adPage);
                if (write)
                {
                    string sourceCode = scraper.getSourceCode(adPage);
                    using (sw = new StreamWriter(filename))
                    {
                        sw.Write(sourceCode);
                    }
                }
            }
            MessageBox.Show("Scrape Complete");

        }
    }
}

####This is the Scraper Object
using System.Net;

namespace Scraper
{
class Scraper
{
    WebClient w = new WebClient();
    public bool UrlExists(string url)
    {
        try
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "HEAD";
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            return (response.StatusCode == HttpStatusCode.OK);
        }
        catch
        {
            return false;
        }
    }

    public string getSourceCode(string url)
    {
        string s = w.DownloadString(url);
        return s;
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:36:54+00:00Added an answer on June 3, 2026 at 10:36 pm

    Found the answer to the problem this morning
    For anyone else having a similar problem, the try catch logic in the UrlExists method needs to close the response (response.Close())
    From what i had understood it autoclosed but this is not the case
    Hope this helps

    Many thanks for the responses in helping me resolve this everyone

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.