日期:2014-05-17  浏览次数:21458 次

System.Net.WebException: 远程服务器返回错误: (405) 不允许的方法
各位同仁:
  我在使用MS介绍其智能客户端时所涉及的两个通用的库 AppStart和AppUpdate进行客户端程序更新,当更新网站在windows2003,XP系统的IIS上均能成功使用,但是在windows server 2008 r2操作系统的IIS7.0 上却不能使用,报错:“System.Net.WebException: 远程服务器返回错误: (405) 不允许的方法”,经过调试,发现报错信息如下:
在 System.Net.WebException 中第一次偶然出现的“System.dll”类型的异常
Error accessing Url http://10.164.1.65/FZLD_Server/UpdateVersion.xml
在 System.Net.WebException 中第一次偶然出现的“AppUpdater.dll”类型的异常
System.Net.WebException: 远程服务器返回错误: (405) 不允许的方法。
   在 System.Net.HttpWebRequest.GetResponse()
   在 Microsoft.Samples.AppUpdater.WebFileLoader.UpdateFile(String url, String filePath) 位置 D:\Work\Project\FZLaborDispatch\AppUpdater\WebFileLoader.cs:行号 109
   在 Microsoft.Samples.AppUpdater.ServerManifest.Load(String url) 位置 D:\Work\Project\FZLaborDispatch\AppUpdater\ServerManifest.cs:行号 50
   在 Microsoft.Samples.AppUpdater.AppUpdater.CheckForUpdates() 位置 D:\Work\Project\FZLaborDispatch\AppUpdater\AppUpdater.cs:行号 277
   在 Microsoft.Samples.AppUpdater.ServerPoller.RunThread() 位置 D:\Work\Project\FZLaborDispatch\AppUpdater\ServerPoller.cs:行号 125

跟踪到代码中,发现在UpdateFile()函数中报错,现将代码公布如下:
public static void UpdateFile(string url, string filePath)
{
HttpWebResponse Response;

//Retrieve the File
HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);
Request.Headers.Add("Translate: f");
Request.Credentials = CredentialCache.DefaultCredentials;

//Set up the last modfied time header
if (File.Exists(filePath)) 
Request.IfModifiedSince = LastModFromDisk(filePath);

try 
{
Response = (HttpWebResponse)Request.GetResponse();
}
catch(WebException e) 
{
if (e.Response == null) 
{
Debug.WriteLine("Error accessing Url " + url);
throw;
}

HttpWebResponse errorResponse = (HttpWebResponse)e.Response;

//if the file has not been modified
if (errorResponse.StatusCode == HttpStatusCode.NotModified)
{
e.Response.Close();
return;
}
else 
{
e.Response.Close();
Debug.WriteLine("Error accessing Url " + url);
throw;
}
}

Stream respStream = null;

try 
{
respStream = Response.GetResponseStream();
CopyStreamToDisk(respStream,filePath);

DateTime d = System.Convert.ToDateTime(Response.GetResponseHeader("Last-Modified"));
File.SetLastWriteTime(filePath,d);

catch (Exception)
{
Debug.WriteLine("APPMANAGER:  Error writing to:  " + filePath);
throw;
}
finally 
{
if (respStream != null)
respStream.Close();
if (Response != null)
Response.Close();
}
}


故障应该发生在Response = (HttpWebResponse)Request.GetResponse();行
请教大家有没有碰到类似的问题。使用的IIS7.0,开始怀疑是权限未配置,但是排查后发现不是权限问题。会是什么原因呢?请大家不吝赐教。