Cornerstone.Tools.WebGrabber.GetString C# (CSharp) Méthode

GetString() public méthode

public GetString ( ) : string
Résultat string
        public string GetString()
        {
            if (response == null)
                return null;

            // If encoding was not set manually try to detect it
            if (encoding == null) {
                try {
                    // Try to get the encoding using the characterset
                    encoding = Encoding.GetEncoding(response.CharacterSet);
                }
                catch (Exception e) {
                    // If this fails default to the system's default encoding
                    logger.DebugException("Encoding could not be determined, using default.", e);
                    encoding = Encoding.Default;
                }
            }

            // Debug
            if (_debug) logger.Debug("GetString: Encoding={2}", encoding.EncodingName);

            // Converts the stream to a string
            try {
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream, encoding, true);
                string data = reader.ReadToEnd();
                reader.Close();
                stream.Close();
                response.Close();

                // return the string data
                return data;
            }
            catch (Exception e) {
                if (e.GetType() == typeof(ThreadAbortException))
                    throw e;

                // There was an error reading the stream
                // todo: might have to retry
                logger.ErrorException("Error while trying to read stream data: ", e);
            }

            // return nothing.
            return null;
        }

Usage Example

 /// <summary>
 /// Prefetches some IMDB details like title and year to assist other data providers
 /// </summary>
 /// <remarks>
 /// We might have to replace/link this to the data provider for IMDB so we don't have redundant logic
 /// </remarks>
 /// <param name="ImdbId"></param>
 /// <returns></returns>
 private static string getImdbDetailsPage(string ImdbId)
 {
     WebGrabber grabber = new WebGrabber("http://www.imdb.com/title/" + ImdbId);
     if (grabber.GetResponse())
         return HttpUtility.HtmlDecode(grabber.GetString());
     else
         return null;
 }
All Usage Examples Of Cornerstone.Tools.WebGrabber::GetString