简体   繁体   English

使用C#ASP.NET时,“WebClient请求期间发生异常”

[英]'An exception occurred during a WebClient request" while using C# ASP.NET

So, I have built an auto update program to my program. 所以,我已经为我的程序构建了一个自动更新程序。

The code that is running in here is: 这里运行的代码是:

new WebClient().DownloadFile("XXXX", checkingfolder.SelectedPath);

the XXX is my webserver that is running as a VPS server in verio, with the newest IIS and everything. XXX是我的网络服务器,在verio中作为VPS服务器运行,具有最新的IIS和所有内容。

When the user clicks on the download button, it says: 当用户点击下载按钮时,它会显示:

'An exception occurred during a WebClient request.

The thing is, that I dont even know why - i am just doing try catch. 问题是,我甚至不知道为什么 - 我只是尝试捕获。

Anyone here have any idea why this happened? 这里的任何人都知道为什么会这样吗?

Thanks for any help you will give me, you have no idea how much you are helping me here - thanks again ! 感谢你给我的任何帮助,你不知道你在这里帮助了我多少 - 再次感谢!

I can reproduce this if I specify, as seems to be the case in your example, a folder name rather than a file name the destination. 我可以重现这个如果我指定,因为似乎是在你的情况为例, 文件夹名,而不是文件名的目标。 Supply a file name instead. 提供文件名。

As an aside; 作为旁白; if I look at the InnerException , it tells me that the problem relates to the file path: 如果我查看InnerException ,它告诉我该问题与文件路径有关:

using(var client = new WebClient())
{
    try
    {
        client.DownloadFile(
            "http://stackoverflow.com/questions/8033619/an-exception-occurred-durning-a-webclient-request-c-sharp-asp-net/8033687#8033687",
            @"j:\MyPath");
    }
    catch (Exception ex)
    {
        while (ex != null)
        {
            Console.WriteLine(ex.Message);
            ex = ex.InnerException;
        }
    }
}

Which gives: 这使:

An exception occurred during a WebClient request.
Access to the path 'j:\MyPath' is denied.

If I change it to a file , it works fine: 如果我将其更改为文件 ,它可以正常工作:

client.DownloadFile(
    "http://stackoverflow.com/questions/8033619/an-exception-occurred-durning-a-webclient-request-c-sharp-asp-net/8033687#8033687",
    @"j:\MyPath\a.html");

有时,当另一个类或进程正在访问您刚刚下载的文件时,可能会发生此错误

it gives exception, if directory to path does not exist. 如果目录不存在,它会给出异常。

for example path is @"j:\\Folder\\SubFolder\\123.pdf and SubFolder does not exist ,it will throw exception. 例如路径是@“j:\\ Folder \\ SubFolder \\ 123.pdfSubFolder不存在,它会抛出异常。

I ran into this error when I was trying to download a file, where the resulting downloaded file path would have been longer than some arbitrary limit 我在尝试下载文件时遇到此错误,其中生成的下载文件路径将长于某个任意限制

After changing the downloaded file's path name to be 250 characters long, the problem went away 将下载的文件的路径名更改为250个字符后,问题就消失了

Beside other answers, beware that the same WebException might also occur if the client process does not have needed permission to create output file. 除了其他答案之外,请注意如果客户端进程没有创建输出文件所需的权限,也可能发生相同的WebException

I would suggest you to take the following strategy: 我建议你采取以下策略:

  1. Download file to a unique filename with .tmp ( .txt ) extensions Windows Temporary Folder to avoid write-permission and other permissions issues 使用.tmp.txt )扩展名将文件下载到唯一的文件名Windows临时文件夹,以避免写入权限和其他权限问题
  2. Move temporary file to destination folder 将临时文件移动到目标文件夹
  3. Rename temporary file to destination filename 将临时文件重命名为目标文件名

Hope it helps :-) 希望能帮助到你 :-)

If Directory does not exist,this error message comes as 'An exception occurred during a WebClient request" Because Web Client Does not find the Folder to store downloaded files. 如果目录不存在,则此错误消息显示为“WebClient请求期间发生异常”,因为Web客户端找不到用于存储下载文件的文件夹。

Hope it Helps-:) 希望能帮助到你-:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用webclient下载大文件时出现“ WebClient请求期间发生异常” - Getting “An exception occurred during a WebClient request” while download the large file using webclient WebClient请求期间发生异常 - Exception occurred during a WebClient request 使用任务计划程序时“WebClient 请求期间发生异常” - "An exception occurred during a WebClient request" when using the Task Scheduler Windows服务WebClient请求期间发生异常 - Windows Service An exception occurred during a WebClient request 执行当前 Web 请求期间发生未处理的异常。 ASP.NET - An unhandled exception occurred during the execution of the current web request. ASP.NET 处理请求时发生未处理的异常。 ASP.NET 核心MVC - An unhandled exception occurred while processing the request. ASP.NET Core MVC 处理请求时发生未处理的异常。(在 asp.net 上排序的其余 api) - An unhandled exception occurred while processing the request.(Rest api sorting on asp.net) WebClient请求期间发生异常。 在Windows Phone中 - An exception occurred during a WebClient request. in windows phone 使用存储过程(SQL数据库)时ASP.NET(C#)捕获异常 - ASP.NET(C#) catching exception while using stored procedure (sql database) 使用C#Web应用程序中的MAPI在ASP.NET中使用默认Web客户端发送邮件 - Send mail using default webclient in ASP.NET using MAPI in C# web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM