Web客户端使用下载文件,以抓住从服务器上的文件 - 处理异常

问题描述:

我有一个Web服务,其中我操纵POST和GET方法,以方便上传/下载功能,在客户机/服务器式建筑的一些文件。基本上,用户可以点击一个按钮来下载特定文件,使应用程序的一些变化,然后点击上传按钮把它送回来。

I have a web service in which I am manipulating POST and GET methods to facilitate upload / download functionality for some files in a client/server style architecture. Basically the user is able to click a button to download a specific file, make some changes in the app, then click upload button to send it back.

问题我有与下载。假设用户希望3个文件1.txt的,2.txt和3.txt。除了2.txt不存在于服务器上。

Problem I am having is with the download. Say the user expects 3 files 1.txt, 2.txt and 3.txt. Except 2.txt does not exist on the server.

所以,我有code等(在服务器端):

So I have code like (on server side):

public class HttpHandler : IHttpHandler
{

    public void ProcessRequest
    {
       if (context.Request.HttpMethod == "GET")
       {
          GoGetIt(context)
       }
    }

private static void GoGetIt(HttpContext context)
{
     var fileInfoOfWhereTheFileShouldBe = new FileInfo(......);

     if (!fileInfoOfWhereTheFileShouldBe.RefreshExists())
     {
          //Remove this line below
          //throw new Exception("Oh dear the file doesn't exist");

          //Replace with a force return of whichever code I chose e.g. 200
          ??...
     }

    ...

所以,我的问题是,当我运行的应用程序,我用一个Web客户端上的客户端使用DownloadFile方法,然后使用code我在上面,我得到:

So the problem I have is that when I run the application, and I use a WebClient on client side to use DownloadFile method which then uses the code I have above, I get:

WebException是未处理:远程服务器返回错误:(500)内部服务器错误

WebException was unhandled: The remote server returned an error: (500) Internal Server Error.

(在调试)。如果我连接到浏览器,并使用的http://本地主机:XXX / 1.txt的我可以通过服务器端code步骤和按计划抛出异常。所以我想我不知道我怎么可以处理在客户端内部服务器错误正确,所以我可以返回一些有意义的事情,如文件不存在。一个想法是使用周围的 WebClient.DownloadFile(地址,文件名)方法一试捕,但我不知道这就是将要发生的,即该文件没有按'唯一的错误牛逼存在。

(While debugging) If I attach to the browser and use http://localhost:xxx/1.txt I can step through server side code and throw the exception as intended. So I guess I'm wondering how I can handle the internal server error on the client side properly so I can return something meaningful like "File doesn't exist". One thought was to use a try catch around the WebClient.DownloadFile(address, filename) method but i'm not sure thats the only error that will occur i.e. the file doesn't exist.

编辑:该解决方案使用如下的Htt presponse

所以,如果我是用的Htt presponse,我能得到一些建议,如何开始?

So if I were to use HttpResponse, could I get some suggestions as how to start?

我删除从客户端的异常抛出,并更换定制的Htt presponse?所以基本上我想我会选择一个code使用,比如200,并迫使回报code 200,如果上述声明。见注释。

I remove the exception throw from the client side, and replace with custom HttpResponse? So basically I guess I would chose a code to use, say 200, and force return code 200 in that if statement above. See comment.

然后在客户端只需使用如果(Response.Status code == 200)和做任何我想做的事(通知用户文件不存在)

Then on client side just use If (Response.StatusCode == 200) and do whatever I want to do (inform user file doesn't exist )

我沿着正确的方向?

修改2:

我一直想用在我的文件拷贝方法尝试捕捉,然后在抓,设置状态code或状态的描述,但设置状态描述..这样的时候,这将引发异常:

I've been trying using a try catch around my file copy methods, then in the catch, setting the status code or status description but this throws exceptions when setting the status description.. like this:

context.Response.StatusDescription = ex.ToString();
context.Response.Status = ex.ToString();

ArgumentOutOfRangeException - 指定的参数已超出有效值范围的

ArgumentOutOfRangeException - specified argument was out of the range of valid values.

如果你正在编写的的IHttpHandler 界面,你不应该扔在了code异常。千万不要!

If you're programming the IHttpHandler interface you shouldn't throw exceptions on that code. Never!

而不是使用的 Response.Status code 和的 Response.StatusDescription 返回的有意义的信息的客户端。

Instead use the Response.StatusCode and Response.StatusDescription to return meaningful information to the client.

让投掷例外仅对系统,因为这样,它会的真的是一个例外, code。

Let the throwing exception only to the system because then, it will REALLY be an exception to YOUR code.

编辑补充

回答您的编辑,我会做到这一点的方法是在情况下在服务器端没有找到将返回 404 状态$ C $的文件C。而让客户端处理这个问题。

Answering to your edit, the way I'd do it would be to in case of a file not found on the server side would return a 404 Status Code. And let the client handle this.

不过,因为你已经把你正在处理的是一个Web服务,所以,我想简单地添加在头一些额外的反应,以便更好地指明到底是什么回事服务器端客户端应用程序。

However, as you have put you're dealing with an web service, so, I'd simply add some additional response in the headers in order to better specify what really is going on the server side to your client application.

编辑补充

Response.Status 是和整数。这就是为什么你得到的 ArgumentOutOfRangeException

Response.Status is and integer. That's why you're getting the ArgumentOutOfRangeException.

请确保状态是有效的 HTTP返回codeS