HttpWebRequest 使用NetworkCredential 进行域认证下载时不成功 的解决方案

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

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值