TrY! TrY!  • 25.11.12 16:41

[Source-Code][C#]Obter Título duma página. Empty [Source-Code][C#]Obter Título duma página. 25.11.12 16:41

Olá,

Criei este script que obtém o código de uma página:

Form1.cs:

Código:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            requerir();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = Clipboard.GetText();
        }
        public void requerir()
        {
            try
            {
                WebClient wc = new WebClient();
                string sc = wc.DownloadString(textBox1.Text);

                progressBar1.Value = 100;

                string title = Regex.Match(sc, @"<title\>([\s\S]*)</title\>", RegexOptions.IgnoreCase).Groups[1].Value;

                string retorno = Encoding.UTF8.GetString(Encoding.GetEncoding("ISO-8859-1").GetBytes(title));


                MessageBox.Show("" + retorno);
               
               
            }
            catch (SystemException e)
            {
                MessageBox.Show("Erro: \n\n" + e.Message, "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

       
    }
}


Form1.Designer.cs:
Código:
namespace WindowsFormsApplication6
{
    partial class Form1
    {
        ///
        /// Required designer variable.
        ///
        private System.ComponentModel.IContainer components = null;

        ///
        /// Clean up any resources being used.
        ///
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        ///
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        ///
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(122, 61);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(32, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "URL:";
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(60, 77);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(172, 20);
            this.textBox1.TabIndex = 1;
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(60, 158);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(155, 44);
            this.button1.TabIndex = 2;
            this.button1.Text = "Confirmar";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(212, 45);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(60, 29);
            this.button2.TabIndex = 3;
            this.button2.Text = "Colar";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // progressBar1
            //
            this.progressBar1.Location = new System.Drawing.Point(32, 217);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(228, 36);
            this.progressBar1.TabIndex = 4;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.progressBar1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.ProgressBar progressBar1;
    }
}


Créditos: TrY

✖Paramaker ✖Paramaker  • 26.11.12 20:49

[Source-Code][C#]Obter Título duma página. Empty Re: [Source-Code][C#]Obter Título duma página. 26.11.12 20:49

Parabéns por ter criado!
É sempre bom haver um programador,
será muito util para alguns!

Obrigado. ;)

Peeta Peeta  • 26.11.12 21:30

[Source-Code][C#]Obter Título duma página. Empty Re: [Source-Code][C#]Obter Título duma página. 26.11.12 21:30

Realmente é um ótimo tutorial parabéns.
Permissões neste sub-fórum
Não podes responder a tópicos

BH Servers

Recomendamos a BH Servers com proteção DDOS gratuita em Cloud Server de alta performance. Entrega imediata.