2
Reply

Streamwriter problem

Nick N

Nick N

Jul 10 2010 1:10 PM
3.1k
Hi,

I am playing around with C# and it seems that I can't get streamwriter to work out properly. I'm not sure what the problem is exactly and can't get any more precise. 

(this is part of a larger program but I chopped it down a bit).

(also, if anybody could tell me how to make it so that the code is actually readable, that would be appreciated.
It makes sense when I'm editing it but the final product is a bit messy) 
 
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {




        //Default DownloadPage from CSHTTP
        static String DownloadPage(Uri url, string line)
        {
            WebProxy proxy = new WebProxy(line, true);
            WebRequest http = HttpWebRequest.Create(url);
            http.Timeout = 1000;
            HttpWebResponse response = (HttpWebResponse)http.GetResponse();
            StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
            String result = stream.ReadToEnd();
            response.Close();
            stream.Close();
            return result;
        }


        static void Main(string[] args)
        {
            //Opening a text stream
            StreamWriter sw = new StreamWriter("goodsites.txt", true);
            StreamReader sr = new StreamReader("fullsites.txt");
            string line = sr.ReadLine();
            while (line != null)
            {

                //Building the URL
                string u = (line);
                Uri url = new Uri(u);
                Console.WriteLine("Sites...");
                Console.WriteLine(line);
                Console.WriteLine("This is the response:");
                try
                {
                    string result = "";
                    result = DownloadPage(url, line);
                    Console.WriteLine(result);
                    if (result != null)
                    {
                        sw.WriteLine(line);
                    }

                }
                catch (UriFormatException e)
                {
                    Console.WriteLine("invalid URL");
                }
                catch (IOException e)
                {
                    Console.WriteLine("invalid");
                }
                catch (WebException webExcp)
                {
                    // To string
                    Console.WriteLine(webExcp.ToString());
                    // Get the WebException status code.
                    WebExceptionStatus status = webExcp.Status;
                    if (status == WebExceptionStatus.ProtocolError)
                    {
                        Console.Write("The server returned protocol error ");
                        // Get HttpWebResponse so that you can check the HTTP status code.
                        HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
                        Console.WriteLine((int)httpResponse.StatusCode + " - "
                           + httpResponse.StatusCode);
                    }
                }

                Console.WriteLine("");
                Console.WriteLine("");
                    line = sr.ReadLine();    
                }
                sr.Close();
                sw.Close();
                Console.WriteLine("All done!");
                Console.ReadKey();

            }
        }
    }


Answers (2)