远程服务器返回错误 (401) 未经授权 解决方案

lvoers 2013-02-25 09:48:10
公司电脑域用户不允许下载,当我使用
 WebRequest req = WebRequest.Create(UpdaterUrl);

WebResponse res = req.GetResponse();

时就报:远程服务器返回错误 (401) 未经授权 解决方案 有人说使用

System.Net.ServicePointManager.ServerCertificatidationCallback = new System.Net.Security.ServerCertificatidationCallback(CheckValidationResult);
,可我的在使用时报System.Net.ServicePointManager不包含ServerCertificatidationCallback 定义,我的是VS2008,不知道各位还有没有其他办法解决。
...全文
2990 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvoers 2013-02-25
  • 打赏
  • 举报
回复
引用 1 楼 ssp2009 的回复:
http://www.cnblogs.com/Jin-1007/archive/2012/04/13/2445596.html
我也就是使用的是它的。可是也还是不行。还是报同样的错误。 代码如下:

   //验证服务器证书回调自动验证
            // System.Net.ServicePointManager.ServerCertificatidationCallback = new System.Net.Security.ServerCertificatidationCallback(CheckValidationResult);
            //使用上面的报 System.Net.ServicePointManager 不包含 ServerCertificatidationCallback 定义。所以选择下面 
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);

            if (!System.IO.Directory.Exists(downpath)) {
                System.IO.Directory.CreateDirectory(downpath);

            }
            string serverXmlFile = downpath + @"/" + clientXml;

            try {
                WebRequest req = WebRequest.Create(UpdaterUrl);
                
                WebResponse res = req.GetResponse();
                if (res.ContentLength > 0) {
                    try {
                        WebClient wClient = new WebClient();
                        wClient.DownloadFile(UpdaterUrl, serverXmlFile);
                    } catch {
                        return string.Empty;
                    }
                }
            } catch (Exception ex) {
                throw ex;
                //MessageBox.Show("获取文件发生异常" + ex.Message,"系统提示");

            }

110,561

社区成员

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

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

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