📄 form1.h
字号:
#pragma once
namespace Example
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Net;
using namespace System::IO;
/// <summary>
/// Form1 摘要
///
/// 警告: 如果您更改该类的名称,则需要更改
/// 与该类所依赖的所有 .resx 文件关联的托管资源编译器工具的
/// “资源文件名”属性。 否则,
/// 设计器将不能与此窗体关联的
/// 本地化资源正确交互。
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Label * label1;
private: System::Windows::Forms::TextBox * textBox1;
private: System::Windows::Forms::TextBox * textBox2;
private: System::Windows::Forms::Label * label2;
private: System::Windows::Forms::Button * button1;
private: System::Windows::Forms::Button * button2;
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container * components;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->label1 = new System::Windows::Forms::Label();
this->textBox1 = new System::Windows::Forms::TextBox();
this->textBox2 = new System::Windows::Forms::TextBox();
this->label2 = new System::Windows::Forms::Label();
this->button1 = new System::Windows::Forms::Button();
this->button2 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// label1
//
this->label1->Location = System::Drawing::Point(32, 32);
this->label1->Name = S"label1";
this->label1->Size = System::Drawing::Size(72, 16);
this->label1->TabIndex = 0;
this->label1->Text = S"网页地址:";
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(104, 24);
this->textBox1->Name = S"textBox1";
this->textBox1->Size = System::Drawing::Size(224, 21);
this->textBox1->TabIndex = 1;
this->textBox1->Text = S"http://www.163.com";
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(104, 56);
this->textBox2->Name = S"textBox2";
this->textBox2->Size = System::Drawing::Size(224, 21);
this->textBox2->TabIndex = 3;
this->textBox2->Text = S"C:\\My163com.htm";
//
// label2
//
this->label2->Location = System::Drawing::Point(32, 64);
this->label2->Name = S"label2";
this->label2->Size = System::Drawing::Size(72, 16);
this->label2->TabIndex = 2;
this->label2->Text = S"保存文件:";
//
// button1
//
this->button1->Location = System::Drawing::Point(56, 88);
this->button1->Name = S"button1";
this->button1->Size = System::Drawing::Size(96, 23);
this->button1->TabIndex = 4;
this->button1->Text = S"下载网页";
this->button1->Click += new System::EventHandler(this, button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(200, 88);
this->button2->Name = S"button2";
this->button2->Size = System::Drawing::Size(96, 23);
this->button2->TabIndex = 5;
this->button2->Text = S"关闭程序";
this->button2->Click += new System::EventHandler(this, button2_Click);
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(6, 14);
this->ClientSize = System::Drawing::Size(360, 134);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->label2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->MaximizeBox = false;
this->Name = S"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = S"演示下载网页内容";
this->ResumeLayout(false);
}
private: System::Void button2_Click(System::Object * sender, System::EventArgs * e)
{//关闭程序
this->Close();
}
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{//下载网页
if(this->textBox1->get_Text()->get_Length()<1|this->textBox2->get_Text()->get_Length()<1)
{
MessageBox::Show("请输入目标网页地址或保存文件名","信息提示",MessageBoxButtons::OK,MessageBoxIcon::Information);
return;
}
String* MyFileName=this->textBox2->get_Text();
String* MyURL=this->textBox1->get_Text();
//加"http://"标志
if(MyURL->IndexOf("http://")==-1)
{
MyURL=String::Concat("http://",MyURL);
}
HttpWebRequest* MyRequest =(HttpWebRequest*)WebRequest::Create(MyURL);
//发送请求,获取响应
HttpWebResponse* MyResponse = (HttpWebResponse*)MyRequest->GetResponse();
Stream* MyInStream;
FileStream* MyFileStream;
try
{
MyInStream =MyResponse->GetResponseStream();
long MyInBytes = MyResponse->get_ContentLength();
//创建文件流对象
MyFileStream=new FileStream(MyFileName,FileMode::OpenOrCreate, FileAccess::Write);
int MyLength = 1024;
unsigned char MyBuffer __gc[]= new unsigned char __gc[1025];
int bytesread = 0;
String* MyTemp="";
//从网络读取数据
while((bytesread =MyInStream->Read(MyBuffer, 0, MyLength)) > 0)
{ //把数据写入文件
MyFileStream->Write(MyBuffer, 0, bytesread);
MyTemp=String::Concat(MyTemp,System::Text::Encoding::Default->GetString(MyBuffer,0,bytesread));
}
MyInStream->Close();
MyFileStream->Close();
MessageBox::Show("下载网页成功!","信息提示",MessageBoxButtons::OK,MessageBoxIcon::Information);
}
catch(Exception* Err)
{
MyInStream->Close();
MyFileStream->Close();
MessageBox::Show(String::Concat("下载网页操作失败!错误是:",Err->get_Message()),"信息提示",MessageBoxButtons::OK,MessageBoxIcon::Information);
}
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -