Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net;
using System.IO;
 
/* I WRITE THIS PROGRAM TO GET WEB CONTENT ON INTERNET , BUT IT NOT WORK CORRECTLY. PLEASE HELP ME */
 

namespace GetHtmlFromInternet
{
    class Program
    {
        public static WebClient wClient = new WebClient();
            public static StreamWriter sw;
            public static TextWriter textWriter;
        /*I USE THIS FUNCTION TO READ WEB PAGE ON INTERNET BUT IT NOT WORK CORRECTLY ON SOME LINKS*/
            public static String readFromLink()
            {
                /* IT WORK CORRECTLY IF WE SET url = "http://vn.yahoo.com/?p=us"; BUT NOT OK WITH URL BELOW*/
                String url = "http://raovat.com/?rv=detail&idrv=527443&idcate=57&tt=vppnhatthanh@yahoo.com.vn";
 
                /* PLEASE HELP ME MAKE FUNCTION READFROMLINK RUNABLE WITH THIS LINK*/
                System.Net.WebClient client = new System.Net.WebClient();
                byte[] data = client.DownloadData(url);
                String html = System.Text.Encoding.UTF8.GetString(data);
                return html;
 

            }
            public static bool WriteTextFile(String fileName, String t)
            {
 
                try
                {
                    textWriter = new StreamWriter(fileName);
                }
                catch (Exception)
                {
                    return false;
                }
 
                try
                {
                    textWriter.WriteLine(t);
                }
                catch (Exception)
                {
                    return false;
                }
                textWriter.Close();
                return true;
            }
        static void Main(string[] args)
        {
            String saveFile = "C:/saveFromURL.txt";
            String reSultString = readFromLink();
            WriteTextFile(saveFile, reSultString);
        }
    }
}
Posted 4-Apr-11 3:20am
gdctld455
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you can simple use the given code to read all webpage
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close()
for more details you can go there[^] and there[^]
  Permalink  
Comments
Albin Abel at 4-Apr-11 10:22am
   
Good answer. My 5
SAKryukov at 4-Apr-11 22:10pm
   
Correct, my 5.
--SA
Kim Togo at 5-Apr-11 3:54am
   
Instead of just assume that encoding always is UTF8, check with myResponse.CharacterSet what encoding is used.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I will do as RaviRanjakr has done. But add some more checking and encoding.
 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
 
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 
if (response.StatusCode == HttpStatusCode.OK)
{
  Stream receiveStream = response.GetResponseStream();
  StreamReader readStream = null;
 
  if (response.CharacterSet == null)
    readStream = new StreamReader(receiveStream);
  else
    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
 
  string data = readStream.ReadToEnd();
  response.Close();
  readStream.Close();
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

// RaviRanjakr , Kim Togo ! It still can not get full page content , please check that link with your browser and run my program with your code insert into the function readFromLink link like this :
public static String readFromLink()
{
String URL = "http://raovat.com/?rv=detail&idrv=527443&idcate=57&tt=vppnhatthanh@yahoo.com.vn";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
String data=null;
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
readStream = new StreamReader(receiveStream);
else readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
data= readStream.ReadToEnd(); response.Close(); readStream.Close();
}
return data;
}
 
// Please help me again and again .I hope get your feedbacks until we can solve this problem!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 5 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100