//Csharp:WebClient and WebRequest use http download file
//20140318 塗聚文收錄
string filePath = "20140302.pdf";
string fileName = "http://www.dusystem.com/3.pdf"; //1出现找不到文件 filePath
//string headerValue = (Request.UserAgent.ToLower().Contains("msie"))? string.Format("attachment; filename=\"{0}\"", Uri.EscapeDataString(fileName)): string.Format("attachment; filename=\"{0}\"", fileName); //for Firefox, Chrome, Safari, Opera
//Response.Clear();
//Response.ContentType = "text/plain";
//Response.AddHeader("Content-Disposition", headerValue);
//Response.TransmitFile(filePath);
//Response.End(); //2: 有效 WebException ex
//System.Net.WebClient net = new System.Net.WebClient();
//string link = fileName;
//Response.ClearHeaders();
//Response.Clear();
//Response.Expires = 0;
//Response.Buffer = true;
//Response.AddHeader("Accept-Language", "utf-8");
//Response.AddHeader("Content-Disposition", "Attachment;FileName=" + System.Web.HttpUtility.UrlEncode(link, System.Text.Encoding.UTF8));
//Response.ContentType = "APPLICATION/octet-stream";
//Response.BinaryWrite(net.DownloadData(link));
//Response.End(); //3:有效,但文件是空
//string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
//string myFileName = dir + @"\213.pdf";
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileName); // //request.MaximumResponseHeadersLength = 40000;
//request.Timeout = 20000;
//request.Credentials = CredentialCache.DefaultCredentials;
//HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//Stream receiveStream = response.GetResponseStream();
//StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); //using (StreamWriter sw = new StreamWriter(myFileName, false, Encoding.UTF8))
//{
// sw.Write(readStream.ReadToEnd());
// sw.Flush();
// sw.Close(); //} //4。有效
//byte[] result;
//byte[] buffer = new byte[4096];
//string link = fileName;
//WebRequest wr = WebRequest.Create(fileName);
//WebResponse response = wr.GetResponse();
//Stream responseStream = response.GetResponseStream();
//MemoryStream memoryStream = new MemoryStream();
//Response.AddHeader("Accept-Language", "utf-8");
//Response.AddHeader("Content-Disposition", "Attachment;FileName=" + System.Web.HttpUtility.UrlEncode(link, System.Text.Encoding.UTF8));
//Response.ContentType = "APPLICATION/octet-stream";
//int count = 0;
//do
//{
// count = responseStream.Read(buffer, 0, buffer.Length);
// memoryStream.Write(buffer, 0, count);
// Response.BinaryWrite(buffer);
// //Response.OutputStream.Write(buffer,0,count);
//}
//while (count != 0);
//result = memoryStream.ToArray(); //5。有效
string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string fullFileName = dir + @"\213.pdf";
HttpWebRequest hRequest = (HttpWebRequest)WebRequest.Create(fileName);
WebResponse response = hRequest.GetResponse();
using (response = (HttpWebResponse)hRequest.GetResponse())
{
/*Download the file to the fullFileName location */
Stream streamResponse = response.GetResponseStream();
if (streamResponse != null)
{
byte[] inBuf = new byte[response.ContentLength];
int bytesToRead = System.Convert.ToInt32(inBuf.Length);
int bytesRead = 0;
while (bytesToRead > 0)
{
int n = streamResponse.Read(inBuf, bytesRead, bytesToRead);
if (n == 0)
{
break;
}
bytesRead += n;
bytesToRead -= n;
} FileStream fstr = new FileStream(fullFileName, FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(inBuf, 0, bytesRead);
streamResponse.Close();
streamResponse.Dispose();
fstr.Close();
fstr.Dispose();
}
}

Csharp:WebClient and WebRequest use http download file的更多相关文章

  1. 第三节:总结.Net下后端的几种请求方式(WebClient、WebRequest、HttpClient)

    一. 前言 前端调用有Form表单提交,ajax提交,ajax一般是用Jquery的简化写法,在这里不再过多介绍: 后端调用大约有这些:WebCient.WebRequest.Httpclient.W ...

  2. c#利用WebClient和WebRequest获取网页源代码的比较

    前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...

  3. C#、.NET网络请求总结(WebClient和WebRequest)

    1.关于WebClient第三方的封装,支持多文件上传等 using System; using System.Collections.Generic; using System.Text; usin ...

  4. c#利用WebClient和WebRequest获取网页源代码

    C#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现.   WebClient类获取网页源代码   WebClient类   WebClient ...

  5. WebClient与WebRequest差异

    WebRequst的使用 WebClient和HttpWebRequst是用来获取数据的2种方式,在我的这篇数据访问(2)中主要是讲的WebClient的使用,一般而言,WebClient更倾向于“按 ...

  6. [Powershell] FTP Download File

    # Config $today = Get-Date -UFormat "%Y%m%d" $LogFilePath = "d:\ftpLog_$today.txt&quo ...

  7. 淘宝开放平台使用WebClient,WebRequest访问时的错误提示导致麻烦

    淘宝开放平台(TOP)提供OAuth2.0支持 通过C#的WebClient/WebRequest直接访问时会提示grant type is empty,这是一个非常恼人的错误,你会发现即使传了这个参 ...

  8. FTP Download File By Some Order List

    @Echo Off REM -- Define File Filter, i.e. files with extension .RBSet FindStrArgs=/E /C:".asp&q ...

  9. WebClient和WebRequest获取html代码

    HTML: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.as ...

随机推荐

  1. 实现反转的方法(reverse)

    1.最简单的方法: public static String reverse1(String str) { return new StringBuffer(str).reverse().toStrin ...

  2. python爬虫之爬取糗事百科并将爬取内容保存至Excel中

    本篇博文为使用python爬虫爬取糗事百科content并将爬取内容存入excel中保存·. 实验环境:Windows10   代码编辑工具:pycharm 使用selenium(自动化测试工具)+p ...

  3. JAVA 集合随笔

    JAVA中提供了现成操纵数据的集合,使得我们在开发中基本不用自己动手实现复杂的数据结构,来保存和操纵数据. 所以当我们有了基本的数据结构知识,会合理利用JAVA提供的集合就好啦. JDK1.8;(仅列 ...

  4. 0、PlayGround可视化

    Tensorflow新手通过PlayGround可视化初识神经网络 是不是觉得神经网络不够形象,概念不够清晰,如果你是新手,来玩玩PlayGround就知道,大神请绕道. PlayGround是一个在 ...

  5. docker-compose部署mysql配置

    docker-compose部署mysql配置文件如下 version: ' services: mysql: image: mysql environment: - MYSQL_ROOT_PASSW ...

  6. 自己动手——快速搭建Android应用服务器

    1.服务器搭建方案选择 我们用:MySQL + Java Web + Tomcat + Volley 来实现我们的服务器. 其中:①MySQL是开源的数据库软件:②Java Web是遵循Java语言风 ...

  7. POJ 2253-Frogger(最小生成树的最大权)

    原题链接:点击此处 题意: 一只叫Freddy的青蛙蹲坐在湖中的一块石头上.突然他发现一只叫Fiona的青蛙在湖中的另一块石头上.Freddy想要跟Fiona约会,但由于湖水太脏,他不想游泳过去而是跳 ...

  8. 进阶篇:5.3.1)均方根法(Root-Sum-Squares,RSS)

    本章目的:了解均方根法,运用均方根法. 1.定义 均方根法(Root-Sum-Squares,RSS):均方根法是统计分析法的一种,是把尺寸链中的各个尺寸公差的平方之和再开根即得到关键尺寸的公差. 其 ...

  9. P3648 [APIO2014]序列分割

    传送门 首先容易证明,得分和切的顺序没有关系 所以直接默认先切左边再切右边就好了 然后显然可以 $dp$ 一开始想的是设 $f[i][j]$ 表示切了 $i$ 次,此次把 $j$ 和 $j+1$ 分开 ...

  10. 针对ACM输出格式的一个小技巧(对格式错误说不!)

    printf("%d%c",bmax," \n"[i==n]); 上文中bmax为题目中需要输出的整形变量,可以脑补很多ans,max之类的,重点在于%c和后面 ...