首页 技术 正文
技术 2022年11月23日
0 收藏 824 点赞 2,604 浏览 4078 个字

1、下载网页源码:

   private void button1_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
string toUrl = Application.StartupPath + "\\" + Path.GetFileName(url); WebClient wc = new WebClient();
wc.DownloadDataAsync(new Uri(url));
wc.DownloadDataCompleted += wc_DownloadDataCompleted;
} void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (e.Error == null && e.Cancelled == false)
textBox2.Text = Encoding.UTF8.GetString(e.Result);
else
MessageBox.Show(e.Error.Message);
}

2、直接下载文件,比较简单,但下载比较伤硬盘:

        private void button1_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
string toUrl = Application.StartupPath + "\\" + Path.GetFileName(url); WebClient wc = new WebClient();
wc.DownloadFileCompleted += wc_DownloadFileCompleted;
wc.DownloadFileAsync(new Uri(url), toUrl);
} void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("下载完成");
}

3、利用缓存下载文件,可以更好的保护硬盘

private void button2_Click(object sender, EventArgs e)
{
WebClient wc = new WebClient();
wc.OpenReadAsync(new Uri(textBox1.Text));
wc.OpenReadCompleted += wc_OpenReadCompleted;
}
async void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
await SaveFile(e.Result, Application.StartupPath + "\\" + Path.GetFileName(textBox1.Text));
MessageBox.Show("下载完成");
}
else
{
MessageBox.Show(e.Error.Message);
}
}
Task SaveFile(Stream stream, string savepath)
{
return Task.Run(() =>
{
var read = stream;
byte[] buf = new byte[];
int res = ;
FileStream fs = File.Open(savepath, FileMode.OpenOrCreate);
using (fs)
{
while ((res = read.Read(buf, , buf.Length)) > )
{
fs.Write(buf, , res);
fs.Flush();
}
}
read.Close();
read.Dispose();
});
}

4、上传文件

web网站创建一般处理程序  FileHandler:

public class FileHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
try
{
HttpFileCollection files = context.Request.Files;
if (files.Count > )
{
files[].SaveAs(HttpContext.Current.Server.MapPath("files/" + context.Request.QueryString["fname"]));
context.Response.Write("save success!");
}
else
context.Response.Write("hello request!");
}
catch (Exception ex)
{
context.Response.Write("save error!" + ex.Message);
}
} public bool IsReusable
{
get
{
return false;
}
}
}

由于网站对上传文件大小有限制,修改上传大小可以在ASP.Net在web.config中设置:

<system.web>
<httpRuntime maxRequestLength="" //即40MB,1KB=1024
useFullyQualifiedRedirectUrl="true"
executionTimeout=""
minFreeThreads=""
minLocalRequestFreeThreads=""
appRequestQueueLimit=""
enableVersionHeader="true"
/>
</system.web>

对于webclient,在winform窗体上点击按钮,则把文件上传到上面的服务器网站目录中。

        private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{ string path = openFileDialog1.FileName;
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.QueryString["fname"] = openFileDialog1.SafeFileName;
byte[] fileb = wc.UploadFile(new Uri(@"http://localhost:15993/FileHandler.ashx"), "POST", path);
string res = Encoding.UTF8.GetString(fileb);
MessageBox.Show(res);
}
}

5、向服务器发送文字

服务器Test.ashx:

 public class Test : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain"; int len=context.Request.ContentLength;
var buf=context.Request.BinaryRead(len);
var res=Encoding.UTF8.GetString(buf); context.Response.Write("Result="+res);
} public bool IsReusable
{
get
{
return false;
}
}
}

客户机:

        private void button2_Click(object sender, EventArgs e)
{
WebClient wc = new WebClient();
var data = textBox2.Text; wc.UploadStringAsync(new Uri("http://localhost:15993/Test.ashx"), "POST", data);
wc.UploadStringCompleted += wc_UploadStringCompleted;
} void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
MessageBox.Show(e.Result);
}

6、上传表单

服务器端用.net  Controller:

 public ActionResult add(int a,int b)
{
int c = a + b;
return Content(c.ToString()); ;
}

客户机:

        private void button3_Click(object sender, EventArgs e)
{
var data = "a=1&b=2";
var postData = Encoding.UTF8.GetBytes(data); WebClient wc = new WebClient();
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.UploadDataAsync(new Uri("http://localhost:15993/home/add"), "POST", postData);
wc.UploadDataCompleted += wc_UploadDataCompleted;
} void wc_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
{
MessageBox.Show(Encoding.UTF8.GetString( e.Result,,e.Result.Length));
}

微信扫一扫

支付宝扫一扫

本文网址:https://www.zhankr.net/140889.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:877 阅读:6,189
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:806 阅读:4,041
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:566 阅读:4,841
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:732 阅读:4,779
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:5,860
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:3,559
助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:8:00-16:00

客服电话

400-888-8888

客服邮箱

ceotheme@ceo.com

扫描二维码

关注微信公众号

扫描二维码

手机访问本站