最近在项目中使用pWebRequest 使用NetworkCredential 进行域认证下载时老不成功,最后Google了解决方案,发现几乎所有讨论的方案都不成功,只好埋头自己解决,最后总算调试通过,特将整个解决过程的代码实现记录下来,节约大家以后解决类似问题的时间。
KEY POINT: 主要是需要加入ContentType让Server端能够正确解码。
request.ContentType = "application/x-www-form-urlencoded";
具体实现代码如下:
void DownloadOneFileByHttp(string remoteurl, string localpath, string localurl)
{
HttpWebRequest
request = HttpWebRequest.Create(remoteurl) as HttpWebRequest;WebRequestMethods.Http.Get; false; new NetworkCredential(this.UserName, this.Password, this.Domain); "application/x-www-form-urlencoded"; //very important for authentication
request.Method =
request.PreAuthenticate =
request.Credentials =
request.ContentType =
MemoryStream memStream = new MemoryStream(1024 * 500);
byte[] buffer = new byte[1024];
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream reader = response.GetResponseStream();
// Create the subfolder
if (!System.IO.File.Exists(localpath))
{
System.IO.
Directory.CreateDirectory(localpath);
}
// Write specified bytes array (content) to a file
FileStream newFile = null;
try
{
newFile =
new FileStream(localurl, FileMode.Create);
while (true)
{
int bytesRead = reader.Read(buffer, 0, buffer.Length);
if(bytesRead == 0)
{
break;
}
else
{
memStream.Write(buffer, 0, bytesRead);
}
}
if(memStream.Length > 0)
{
// Converts the downloaded stream to a byte array
byte [] downloadedData = memStream.ToArray();
newFile.Write(downloadedData, 0, downloadedData.Length);
}
}
catch (Exceptionex)
{
System.
Console.WriteLine("Exception in HTTP downloading: {0}", ex.Message);
}
finally
{
if (newFile != null) newFile.Close();
if (reader != null) reader.Close();
if (response != null) response.Close();
}
}
最后提示一下大家:对于想了解为何要这样写的朋友可以使用IE登陆并且用httpwatchpro 拦截一下整个报文的内容,然后用.NET代码访问并且下载同样的文件,然后使用winpcap这样的网络嗅探器拦截一下整个报文的内容,对比一下你就很容易得出结论。
当然这个解决方案对于熟知HTTP协议并且编写过自定义爬虫的朋友是很容易得出。
分享到:
相关推荐
本文将讲述如何采用HttpWebRequest方式每次读取固定大小数据片段(如4KB)发送至服务器,为大文件上传提供解决方案,本文还将详细讲述如何将“文件上传”功能做为用户自定义控件,实现模块重用。
C# 多线程基本于httpwebrequest实现的下载功能 每个线程均可以通过事件追踪情况
C# asp.net http HttpWebRequest模拟浏览器请求下载文件到本地
HttpWebRequest详解HttpWebRequest详解HttpWebRequest详解
例如,当您从响应很慢的API服务器下载大文件时,您的应用程序的UI不会停止响应。HttpWebRequest通常和WebResponse一起使用,一个发送请求,一个获取数据。HttpWebRquest更为底层一些,能够对整个访问过程有个直观的...
解决火车头7.6网站报错System.Net.HttpWebRequest问题的处理 插件 无限制。 https.dll 永久使用无限制版本https
本文将讲述如何采用HttpWebRequest方式每次读取固定大小数据片段(如4KB)发送至服务器,为大文件上传提供解决方案,本文还将详细讲述如何将“文件上传”功能做为用户自定义控件,实现模块重用。
利用HttpWebRequest+HtmlAgilityPack进行数据采集,开发环境:Visual Studio 2010,.Net Framework 4.0
C#使用HttpWebRequest自动更新客户端应用程序
使用 HttpWebRequest发送自定义POST请求
图片批量下载器 利用正则表达式和HttpWebRequest批量下载对应网页的图片。 功能虽简单,但重要的是方法。
httpwebrequest调用webservice返回数据
使用HttpWebRequest提交数据
本示例使用C#构造SOAP信息,通过HttpWebRequest调用java编写的带有Windows身份验证的WebService,代码中详细注释了每行代码的功能与作用; 对应文章:http://blog.csdn.net/cgs_______/article/details/77894599
C#中HttpWebRequest使用介绍 GET HTTP HTTPS 请求
这个东东有两个用处 1.这是一个用HttpWebRequest类构建完整...2.上传地址是金山文档预览的地址,所以,只要上传的是word、pdf、excel一类的文档,可以通过返回的地址直接在线预览,是一种偷懒的文档在线浏览解决方案。
httpwebrequest 封装类
window phone WebClient和HttpWebRequest 并解决gb2312乱码问题
Asp.Net使用HttpWebRequest跨域上传,可以实现asp.net跨域文件上传或者大数传输
BreakpointTransmission 分段上传,在此基础上加入下载模块。略粗糙,换点分。