HttpWebRequest 使用NetworkCredential 进行域认证下载时不成功 的解决方案
2010-05-20 10:34
381 查看
最近在项目中使用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 (Exception ex)
{
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协议并且编写过自定义爬虫的朋友是很容易得出。
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 (Exception ex)
{
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协议并且编写过自定义爬虫的朋友是很容易得出。
相关文章推荐
- C#使用HttpWebRequest 进行get 和post提交
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
- SpringMVC上传图片总结(1)-Web Uploader--常规方法进行图片上传,使用了MultipartFile、MultipartHttpServletRequest
- (微信API接口开发) 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
- httpwebrequest下载文件失败的解决方案
- 使用HttpWebRequest下载远程文件部分网站出现异常
- 使用HttpWebRequest和HttpWebResponse在后台进行文件的请求和响应
- 每日一则JavaWeb---spring中非Controller层使用HttpServletRequest两种解决方案
- C#中网页文件下载——HttpWebRequest与HttpWebResponse的使用方法
- Unity内使用HttpWebRequest下载数据,缓存问题的解决
- HttpWebRequest 下载网页Html代码 POST方式(站内使用了form方式) System.Net.WebException (417) Expectation failed
- HttpWebRequest下载文件,乱码问题解决方案
- 使用WebClient HttpWebRequest 的高级应用,获取带验证的session下载权限
- 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
- 使用 HttpWebRequest 下载任意类型的文件
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
- 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
- 反射:修改请求头HttpWebRequest/Webclient Header属性的date值-"此标头必须使用适当的属性进行修改"
- 使用HttpWebRequest远程下载文件