📄 sample3.cs
字号:
namespace apiBook
{
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Windows.Forms;
public class TestWebClientClass
{
public static void Main()
{
string uriStr = "c:\\test.xml";
WebClient testWC = new WebClient();
Console.WriteLine("下载" + uriStr+"的数据");
byte[] testBuffer = testWC.DownloadData (uriStr);
//使用DownloadData方法从具有指定 URI 的资源下载数据
string content = Encoding.ASCII.GetString(testBuffer);
Console.WriteLine(content);
Console.WriteLine("下载完成。");
string fnStr = "test.xml";
string fullPath = null;
string uriFStr="c:\\";
testWC = new WebClient();
fullPath =uriFStr+ fnStr;
Console.WriteLine("从"+fullPath+"下载文件"+fnStr);
testWC.DownloadFile(fullPath,fnStr);
//使用DownloadFile方法从具有指定 URI 的资源将数据下载到本地文件
Console.WriteLine("完成从"+fullPath+"下载文件"+fnStr+"的任务。");
Console.WriteLine("下载的文件存放在目录:"+Application.StartupPath);
Console.Write("使用POST方法将文件"+fullPath+"上传到d:\\");
testWC=new WebClient();
uriStr="d:\\";
Console.WriteLine("上传"+fullPath+"文件到"+uriStr);
byte[] testA =testWC.UploadFile(uriStr,"POST",fullPath);
//使用UploadFile方法将本地文件上载到具有指定URI 的资源
Console.WriteLine("文件上传完成。");
uriStr = "c:\\test.xml";
testWC = new WebClient();
Console.WriteLine("读取"+uriStr+"的内容:");
Stream testS = testWC.OpenRead(uriStr);
//使用OpenRead方法为从具有指定 URI 的资源下载的数据打开一个可读的流
StreamReader reader = new StreamReader(testS);
Console.WriteLine(reader.ReadToEnd());
reader.Close();
string data ="new content";
testA = Encoding.ASCII.GetBytes(data);
testWC = new WebClient();
Console.WriteLine("更新"+uriStr+"内容");
Console.WriteLine("写入内容为:"+data);
testS=testWC.OpenWrite(uriStr,"POST");
//使用OpenWrite方法打开一个流以将数据写入具有指定 URI 的资源
testS.Write(testA,0,testA.Length);
testS.Close();
Console.WriteLine("数据更新成功。");
Console.ReadLine();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -