Nick N

Nick N

  • NA
  • 1
  • 4.5k

Streamwriter problem

Jul 10 2010 1:10 PM
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) 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net;  
  6. using System.IO;  
  7. namespace ConsoleApplication1  
  8. {  
  9. class Program  
  10. {  
  11.     //Default DownloadPage from CSHTTP  
  12.     static String DownloadPage(Uri url, string line)  
  13.     {  
  14.         WebProxy proxy = new WebProxy(line, true);  
  15.         WebRequest http = HttpWebRequest.Create(url);  
  16.         http.Timeout = 1000;  
  17.         HttpWebResponse response = (HttpWebResponse)http.GetResponse();  
  18.         StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);  
  19.         String result = stream.ReadToEnd();  
  20.         response.Close();  
  21.         stream.Close();  
  22.         return result;  
  23.     }  
  24.     static void Main(string[] args)  
  25.     {  
  26.         //Opening a text stream  
  27.         StreamWriter sw = new StreamWriter("goodsites.txt"true);  
  28.         StreamReader sr = new StreamReader("fullsites.txt");  
  29.         string line = sr.ReadLine();  
  30.         while (line != null)  
  31.         {  
  32.             //Building the URL  
  33.             string u = (line);  
  34.             Uri url = new Uri(u);  
  35.             Console.WriteLine("Sites...");  
  36.             Console.WriteLine(line);  
  37.             Console.WriteLine("This is the response:");  
  38.             try  
  39.             {  
  40.                 string result = "";  
  41.                 result = DownloadPage(url, line);  
  42.                 Console.WriteLine(result);  
  43.                 if (result != null)  
  44.                 {  
  45.                     sw.WriteLine(line);  
  46.                 }  
  47.             }  
  48.             catch (UriFormatException e)  
  49.             {  
  50.                 Console.WriteLine("invalid URL");  
  51.             }  
  52.             catch (IOException e)  
  53.             {  
  54.                 Console.WriteLine("invalid");  
  55.             }  
  56.             catch (WebException webExcp)  
  57.             {  
  58.                 // To string  
  59.                 Console.WriteLine(webExcp.ToString());  
  60.                 // Get the WebException status code.  
  61.                 WebExceptionStatus status = webExcp.Status;  
  62.                 if (status == WebExceptionStatus.ProtocolError)  
  63.                 {  
  64.                     Console.Write("The server returned protocol error ");  
  65.                     // Get HttpWebResponse so that you can check the HTTP status code.  
  66.                     HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;  
  67.                     Console.WriteLine((int)httpResponse.StatusCode + " - "  
  68.                        + httpResponse.StatusCode);  
  69.                 }  
  70.             }  
  71.             Console.WriteLine("");  
  72.             Console.WriteLine("");  
  73.                 line = sr.ReadLine();      
  74.             }  
  75.             sr.Close();  
  76.             sw.Close();  
  77.             Console.WriteLine("All done!");  
  78.             Console.ReadKey();  
  79.         }  
  80.     }  
  81. }  


Answers (2)