System.Net.WebException: 远程服务器返回错误: (405) 不允许的方法

joeyxiaobin 2013-02-06 10:41:46
各位同仁:
我在使用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,开始怀疑是权限未配置,但是排查后发现不是权限问题。会是什么原因呢?请大家不吝赐教。
...全文
3849 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xb网络 2013-06-11
  • 打赏
  • 举报
回复
对了,我用的是多线程,测试时出错后 vs2012把错误定位在多线程的异步回调函数上
 public void MyAsyncCallback(IAsyncResult ar)
        {
            string s;
            int iExecThread;

            // Because you passed your original delegate in the asyncState parameter
            // of the Begin call, you can get it back here to complete the call.
            MethodDelegate dlgt = (MethodDelegate)ar.AsyncState;

            // Complete the call.
            s = dlgt.EndInvoke(out iExecThread, ar);
            //MessageBox.Show(String.Format("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString()));

            //Console.WriteLine(string.Format ("The delegate call returned the string:   "{0}", and the number {1}", s,iExecThread.ToString() ) );
        }
xb网络 2013-06-11
  • 打赏
  • 举报
回复
这个错误是不经常发生,时间不定,请问,我是自己写的一个程序winForm程序,应该怎样处理
qldsrx 2013-02-10
  • 打赏
  • 举报
回复
那个Stream不支持查看长度,你怎么知道是0?
qldsrx 2013-02-08
  • 打赏
  • 举报
回复
文件扩展名:.xml MIME类型:text/xml
joeyxiaobin 2013-02-08
  • 打赏
  • 举报
回复
引用 9 楼 net_lover 的回复:
你的url http://10.164.1.65/FZLD_Server/UpdateVersion.xml 在浏览器中如果能访问,HttpWebRequest也应该能够直接访问的,采用GET方法应该是可以的,服务器默认设置就可以,无需特殊设置
嗯,没错,我把IIS7.0删掉重新安装,使用默认安装,现在HttpWebRequest.GetResponse()可以获取了,但是现在又有一个新问题,我要获取http://10.164.1.65/FZLD_Server/UpdateVersion.xml的文件,在网页浏览中有内容,但是我使用HttpWebResponse.GetResponseStream()获取后,获取到的Stream长度却为0,这事为什么???? 急死我了,谢谢大家
孟子E章 2013-02-08
  • 打赏
  • 举报
回复
你的url http://10.164.1.65/FZLD_Server/UpdateVersion.xml 在浏览器中如果能访问,HttpWebRequest也应该能够直接访问的,采用GET方法应该是可以的,服务器默认设置就可以,无需特殊设置
机器人 2013-02-07
  • 打赏
  • 举报
回复
估计是没有对 .xml 进行meta映射的原因 http://blog.csdn.net/WhatWhoWhere/article/details/6033616
joeyxiaobin 2013-02-07
  • 打赏
  • 举报
回复
有人知道*.xml在处理程序映射中,可执行程序 怎么配置吗? 谢谢大家。
  • 打赏
  • 举报
回复
你用的多线程操作文件吗? 如果是的话那就是操作文件时候起冲突了。 是不是有时候会发生。有时候是正常的?
joeyxiaobin 2013-02-07
  • 打赏
  • 举报
回复
引用 5 楼 kongwei521 的回复:
选择站点-》IIS-》处理程序映射-》添加脚本映射(对应扩展名) http://www.cnblogs.com/tearer/archive/2012/10/24/2737823.htmlIIS7.5 伪静态 脚本映射 配置方法 http://technet.microsoft.com/zh-cn/library/cc771240
你好,我在设置 处理程序映射-》添加脚本映射 设置*.xml的脚本映射,但是不知道可执行程序选择什么,从网上搜索没看到,第一次接触这个,感觉7.0比5.1复杂多了
蝶恋花雨 2013-02-07
  • 打赏
  • 举报
回复
选择站点-》IIS-》处理程序映射-》添加脚本映射(对应扩展名) http://www.cnblogs.com/tearer/archive/2012/10/24/2737823.htmlIIS7.5 伪静态 脚本映射 配置方法 http://technet.microsoft.com/zh-cn/library/cc771240
joeyxiaobin 2013-02-07
  • 打赏
  • 举报
回复
引用 2 楼 lye2000000_super 的回复:
你用的多线程操作文件吗? 如果是的话那就是操作文件时候起冲突了。 是不是有时候会发生。有时候是正常的?
好像不是多线程冲突,应该是*.xml未设置处理程序映射。
joeyxiaobin 2013-02-07
  • 打赏
  • 举报
回复
引用 1 楼 fangxinggood 的回复:
估计是没有对 .xml 进行meta映射的原因 http://blog.csdn.net/WhatWhoWhere/article/details/6033616
好像是这个原因,但是我的url是http://10.164.1.65/FZLD_Server/UpdateVersion.xml,请问*.xml的程序映射的可执行文件是什么?谢谢

111,082

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧