Click here to Skip to main content
15,885,979 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
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
Updated 6-Oct-19 5:35am

// 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!
 
Share this answer
 
I will do as RaviRanjakr has done. But add some more checking and encoding.


C#
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();
}
 
Share this answer
 
you can simple use the given code to read all webpage
C#
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[^]
 
Share this answer
 
Comments
Albin Abel 4-Apr-11 10:22am    
Good answer. My 5
Sergey Alexandrovich Kryukov 4-Apr-11 22:10pm    
Correct, my 5.
--SA
Kim Togo 5-Apr-11 3:54am    
Instead of just assume that encoding always is UTF8, check with myResponse.CharacterSet what encoding is used.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900