首页 > 编程知识 正文

httpclient close,web服务器的工作原理

时间:2023-05-06 00:49:59 阅读:173575 作者:1086

同时,WebClient (万维网客户端)提供了向用URI标识的资源发送数据和从用URI标识的资源接收数据的通用方法,其中WebClient类是用URI标识的任何本地、因特网或

WebClient类使用WebRequest类提供对资源的访问。 WebClient实例可以通过在WebRequest.RegisterPrefix方法中注册的任何WebRequest子代访问数据。 缺省情况下,WebClient实例不发送可选的HTTP标头。

如果请求需要可选标头,则必须将标头添加到Headers集合中。 例如,要在响应中保留查询,必须添加用户代理标头。 此外,如果用户代理的标头丢失,服务器可能返回500 (内部服务器错误)。

在WebClient实例中,AllowAutoRedirect设置为true。

看一下webclient在MSND上的解释,有上传和下载的方法。

WebClient提供以下上载方法:

WebClient提供以下下载方法:

二、WebClient与其他网络相关类的区别WebClient和HttpWebRequst是获取数据的两种方式,一般来说,WebClient倾向于“按需下载”,实际掌握它

后者类似于form的submit。 虽然这两个请求都是异步请求事件,但WebClient是基于事件的异步,而HttpWebRequst是基于代理的异步编程。

三. WebClient使用样例文件下载:

protectedvoidbutton1_ click (object sender,EventArgs e ) string URL=' http://localhost/sprayer web/download/1.zip ' WIP ' byte [ ] bytes=webclient.download data (URL; string path=string.format (({0}{1}) {2}、server.mappath )、((download )、datetime filestream fs fs.write(bytes,0,bytes.Length ); fs.Close (; } protectedvoidbutton2_ click (object sender,EventArgs e ) webclient webclient=new webclient ); string URL=' http://localhost/sprayer web/download/1.zip '; streamstream=webclient.openread (URL; string path=string.format (({0}{1}) {2}、server.mappath )、((download )、datetime filestream fs byte[] bytes=new byte[8192]; int read=0; while((read=stream.read ) bytes,0,bytes.Length ) )0) {fs.write ) bytes,0,read; //此处需要read,bytes.Length } fs.Close ); } protectedvoidbutton3_ click (object sender,EventArgs e ) webclient webclient=new webclient ); string URL=' http://localhost/sprayer web/download/1.zip '; string path=string.Format

("{0}\{1}\{2}{3}", Server.MapPath("~"), "download", DateTime.Now.ToString("yyyyHHmmHHmmssfff"), ".zip"); webclient.DownloadFile(url, path); } protected void Button4_Click(object sender, EventArgs e) { String url = "http://localhost/SprayerWeb/download/1.zip"; Uri uri = new Uri(url, UriKind.Absolute); WebClient webclient = new WebClient(); webclient.OpenReadAsync(uri); webclient.OpenReadCompleted+=webclient_OpenReadCompleted; } private void webclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { Stream stream = e.Result; byte[] bytes = new byte[8192]; int read = 0; string path = string.Format("{0}\{1}\{2}{3}", Server.MapPath("~"), "download", DateTime.Now.ToString("yyyyHHmmHHmmssfff"), ".zip"); FileStream fs = new FileStream(path, FileMode.Create); while ((read = stream.Read(bytes, 0, bytes.Length)) > 0) { fs.Write(bytes, 0, read); } fs.Close(); stream.Close(); }
文件上传:

protected void Button5_Click(object sender, EventArgs e) { //在IIS6下没有执行成功,以后再调试吧 WebClient webclient = new WebClient(); string targetpath = string.Format("{0}{1}{2}","http://localhost/WebApp/upload/",DateTime.Now.ToString("yyyyMMddHHmmssfff"),".zip"); string sourcepath = string.Format("{0}\{1}\{2}{3}", Server.MapPath("~"), "download", "1", ".zip"); byte[] responseArray = webclient.UploadFile(targetpath, "PUT", sourcepath); } protected void Button6_Click(object sender, EventArgs e) { //在IIS6下没有执行成功,以后再调试吧 string targetpath = string.Format("{0}{1}{2}", "http://localhost/WebApp/upload/", DateTime.Now.ToString("yyyyMMddHHmmssfff"), ".zip"); string sourcepath = string.Format("{0}\{1}\{2}{3}", Server.MapPath("~"), "download", "1", ".zip"); WebClient webclient = new WebClient(); FileStream fs = new FileStream(sourcepath, FileMode.Open, FileAccess.Read); byte[] bt = new byte[fs.Length]; fs.Read(bt, 0, bt.Length); byte[] responseArray = webclient.UploadData(targetpath, "PUT", bt); fs.Close(); } protected void Button7_Click(object sender, EventArgs e) { WebClient webclient = new WebClient(); string sourcepath = string.Format("{0}\{1}\{2}{3}", Server.MapPath("~"), "download", "1", ".zip"); byte[] responseArray= webclient.UploadFile("http://localhost/WebApp/upload.aspx", "POST", sourcepath); Response.Write("result:"+System.Text.Encoding.UTF8.GetString(responseArray)); }
upload.aspx文件代码如下:

protected void Page_Load(object sender, EventArgs e) { //string uploadpath = string.Format("{0}\{1}{}"); foreach (string f in Request.Files.AllKeys) { HttpPostedFile postfile = Request.Files[f]; string path = String.Format("{0}\{1}\{2}{3}",Server.MapPath("~"),"upload",DateTime.Now.Ticks.ToString(),".zip"); postfile.SaveAs(path); } Response.Write("Success"); }


更多详细内容参考:https://msdn.microsoft.com/zh-cn/library/system.net.webclient%28VS.80%29.aspx



版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。