最近在项目中使用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协议并且编写过自定义爬虫的朋友是很容易得出。