CSDN博客

img lovebanyi

C#网页源代码获取

发表于2005/1/2 11:01:00  2949人阅读

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Text;
using System.IO;

namespace Banyiweb
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox textBoxWeb;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.StatusBar statusBar1;
  private System.Windows.Forms.RichTextBox richTextBox1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TOD 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.label1 = new System.Windows.Forms.Label();
   this.textBoxWeb = new System.Windows.Forms.TextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.statusBar1 = new System.Windows.Forms.StatusBar();
   this.richTextBox1 = new System.Windows.Forms.RichTextBox();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(0, 16);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(72, 16);
   this.label1.TabIndex = 0;
   this.label1.Text = "请输入网址:";
   //
   // textBoxWeb
   //
   this.textBoxWeb.Location = new System.Drawing.Point(80, 8);
   this.textBoxWeb.Name = "textBoxWeb";
   this.textBoxWeb.Size = new System.Drawing.Size(248, 21);
   this.textBoxWeb.TabIndex = 1;
   this.textBoxWeb.Text = "http://www.";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(344, 8);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(56, 20);
   this.button1.TabIndex = 2;
   this.button1.Text = "提交";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // statusBar1
   //
   this.statusBar1.Location = new System.Drawing.Point(0, 351);
   this.statusBar1.Name = "statusBar1";
   this.statusBar1.Size = new System.Drawing.Size(416, 22);
   this.statusBar1.TabIndex = 5;
   this.statusBar1.Text = "http://www.banyi.com/txxl  by: Banyi";
   //
   // richTextBox1
   //
   this.richTextBox1.Location = new System.Drawing.Point(0, 32);
   this.richTextBox1.Name = "richTextBox1";
   this.richTextBox1.Size = new System.Drawing.Size(416, 320);
   this.richTextBox1.TabIndex = 6;
   this.richTextBox1.Text = "请正确输入网址,在程序找不到网址的时候会变慢";
   //
   // Form1
   //
   this.AutoScale = false;
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(416, 373);
   this.Controls.Add(this.richTextBox1);
   this.Controls.Add(this.statusBar1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.textBoxWeb);
   this.Controls.Add(this.label1);
   this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
   this.Name = "Form1";
   this.Text = "网页源代码";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void button1_Click(object sender, System.EventArgs e)
  {  
   string webstr;
   webstr=textBoxWeb.Text;
   if (webstr.IndexOf("http:") <0)
   {
    MessageBox.Show("出错了 BANYI","你输入的网址有误");
    textBoxWeb.Text="http://www.";
    return;
   }
   button1.Enabled=false;
   HttpWebResponse res;
   HttpWebRequest req;
   try
   { req=(HttpWebRequest)WebRequest.Create(webstr);
    res=(HttpWebResponse)req.GetResponse();
   }
   catch (Exception)
   {
    MessageBox.Show("出错了 BANYI","对不起找不到该网址");
    button1.Enabled=true;
    return;
   }
    textBoxWeb.Text="正在读取中....";
    StreamReader strm=new StreamReader(res.GetResponseStream(),Encoding.GetEncoding(54936));//简体中文 Encoding.GetEncoding(54936),Encoding.ASCII
    String sLine;
    richTextBox1.Text="";
    
    do  //学习笔记do循环C# do while 循环体 loop  do until 循环体 loop
    {
     sLine=strm.ReadLine();
     richTextBox1.Text=richTextBox1.Text+sLine;
    }
    while (sLine!=null);
    strm.Close();
    textBoxWeb.Text="http://www.";
    button1.Enabled=true;
  
  }

}
}
这边就是改了一个编码的问题。这样才可以获得中文Encoding.GetEncoding(54936)

0 0

相关博文

我的热门文章

img
取 消
img