【问题标题】:Uploading files to server上传文件到服务器
【发布时间】:2010-10-15 03:44:48
【问题描述】:

我正在尝试使用 C# 将文件从我的 Windows 应用程序上传到服务器到特定文件夹中。但是,我遇到了一个例外:

“WebClient 请求期间发生异常”。

这是我的代码:

for (int i = 0; i < dtResponseAttach.Rows.Count; i++)
{
  string filePath = dtResponseAttach.Rows[i]["Response"];

  WebClient client = new WebClient();
  NetworkCredential nc = new NetworkCredential();

  Uri addy = new Uri("http://192.168.1.4/people/Attachments/");
  client.Credentials = nc;
  byte[] arrReturn = client.UploadFile(addy, filePath);
  Console.WriteLine(arrReturn.ToString());
}

这个异常的原因可能是什么?

【问题讨论】:

  • 您是否查看过 InnerException 或查询 Exception 流以获取更多信息。
  • 是的,我同意。 “在 WebClient 请求期间发生异常”并不能告诉我们很多内容

标签: c# exception webclient file-upload


【解决方案1】:

如果你没有填写NetworkCredential,那么我很确定你不应该附上。

另一种可能性是您正在通过代理,并且需要添加代理详细信息:

WebProxy p = new WebProxy ("192.168.10.01", true);
p.Credentials = new NetworkCredential ("username", "password", "domain");
using (WebClient wc = new WebClient())
{
  wc.Proxy = p;
  ...
}

【讨论】:

    猜你喜欢
    • 2011-01-27
    • 2015-02-27
    • 2015-04-09
    • 2014-01-31
    • 2012-02-06
    • 2014-10-27
    • 2017-06-02
    相关资源
    最近更新 更多