我应该检查 WebClient.UploadFile 的响应来了解上传是否成功吗?

发布于 2024-10-07 22:28:03 字数 130 浏览 0 评论 0原文

我以前从未使用过WebClient,我不确定是否应该检查服务器的响应以了解上传是否成功,或者如果没有异常我是否可以让文件上传。

如果我应该检查回复,我该怎么做?解析 resposeHeaders 属性?

提前致谢。

I never used the WebClient before and I'm not sure if I should check the response from the server to know if the upload was successful or if I can let the file as uploaded if there is no exception.

If I should check the response how can I do that? Parsing resposeHeaders property?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

予囚 2024-10-14 22:28:03

UploadFile 方法返回一个 byte[],其中包含远程服务器返回的响应。根据服务器管理上传请求响应的方式(以及错误条件(参见下面的注释 1)),您将需要检查该响应。您可以通过将其转换为字符串来获取字符串响应,例如,这会将响应写入控制台窗口:

byte[] rawResponse = webClient.UploadFile(url,fileName);
Console.WriteLine("Remote Response: {0}", System.Text.Encoding.ASCII.GetString(rawResponse));

也就是说,如果远程服务器返回除 HTTP 200 以外的任何内容(即成功)调用 UploadFile 将抛出 WebException。您可以以最适合您的应用程序的方式捕获并处理它。

因此,将所有这些放在一起

try
{
    WebClient webClient = new WebClient();
    byte[] rawResponse = webClient.UploadFile(url,fileName);

    string response = System.Text.Encoding.ASCII.GetString(rawResponse);

    ...
    Your response validation code
    ...
}
catch (WebException wexc)
{
    ...
    Handle Web Exception
    ...
}

注释 1 作为示例,我有一个文件上传服务,除了 HTTP 200 代码之外,永远不会发出任何其他内容,所有错误都会在服务中捕获,并且这些错误会被“解析”为返回给调用者的 XML 结构。然后调用者解析该 XML 以验证上传是否成功。

The UploadFile method returns a byte[] that contains the response the remote server returned. Depending on how the server manages responses to upload requests (and error conditions (see note 1 below)) you will need to check that response. You can get the string response by converting it to a string, for example this will write the response to the console window:

byte[] rawResponse = webClient.UploadFile(url,fileName);
Console.WriteLine("Remote Response: {0}", System.Text.Encoding.ASCII.GetString(rawResponse));

That said if the remote server returns anything other than a HTTP 200 (i.e. success) the call to UploadFile will throw a WebException. This you can catch and deal with it whatever manner best suits your application.

So putting that all together

try
{
    WebClient webClient = new WebClient();
    byte[] rawResponse = webClient.UploadFile(url,fileName);

    string response = System.Text.Encoding.ASCII.GetString(rawResponse);

    ...
    Your response validation code
    ...
}
catch (WebException wexc)
{
    ...
    Handle Web Exception
    ...
}

Note 1 As an example I have a file upload service that will never issue anything other than a HTTP 200 code, all errors are caught within the service and these are "parsed" into an XML structure that is returned to the caller. The caller then parses that XML to validate that the upload was successful.

单调的奢华 2024-10-14 22:28:03

如果上传返回的 StatusCode 不是 200(或 200 范围),则 WebClient.UploadFile 应引发 WebException。

作为插件,我在 BizArk 上有一个代码参考库,其中包含一个 WebHelper 类,可以轻松同时上传多个文件和表单值。该项目名为 BizArk

If the upload returns a StatusCode other than 200 (or 200 range), WebClient.UploadFile should raise a WebException.

As a plug, I have a code reference library on BizArk that includes a WebHelper class that makes it easy to upload multiple files and form values at the same time. The project is called BizArk.

提笔落墨 2024-10-14 22:28:03

在 msdn 提供的示例中,他们检查了响应,因此可能会很好风格,但我倾向于不自己做,而且还没有被烧伤。

In the examples provided at msdn they check the response so it might be good style, but I tend not to do it myself and haven't gotten burned yet.

寄人书 2024-10-14 22:28:03

您还可以使用异步方法 UploadFileAsync 并检查事件处理程序 UploadFileCompletedEventHandler 中发生的事件 UploadFileCompleted 的结果。您可能必须添加额外的代码来进行同步。

You also can use async method UploadFileAsync and check results in event handler UploadFileCompletedEventHandler occurred from event UploadFileCompleted. You probably have to add additional code for synchronization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文