日期:2014-05-17  浏览次数:20920 次

C# WebClient上传下载资料问题求助
各位大哥大姐,菜鸟 现求助问题:如下是一段从服务器如\\192.149.200.50下载一个文件,但是现在发现,下载登录服务器失败,提示信息:“未知的用户名与密码“。手动登录到\\192.149.200.50,下载则是没有问题,所以应该是我登录到服务器的有问题,请教各位大侠,如何根据用户名和密码登录到这个\\192.149.200.50服务器上。很急,谢谢帮忙。
  //Download file
  public static bool DownloadFile(string urlString,string fileName,string fielPath)
  {

  //<summary>
  //下载服务器文件至客户端
  //</summary>
  //<param name="URL">被下载的文件地址,绝对路径</param>
  //<param name="fielPath">绝对路径</param>
  string URL = null;
  if (urlString.EndsWith(@"\") == false)
  {
  URL = urlString + @"\";
  }
  URL = URL + fileName;

  WebRequest myre = WebRequest.Create(URL);
  WebClient myWebClient = new WebClient();
  myWebClient.UseDefaultCredentials = true;

  myWebClient.Credentials = new NetworkCredential("X", "X");
   
  //string fileName = URL.Substring(URL.LastIndexOf("\\") + 1); //被下载的文件名

  try
  {
  myWebClient.DownloadFile(URL, fielPath);
  }
  catch(Exception ex)
  {

  Console.WriteLine(ex.Message);
  return false;
  }
   
  return true;
  }

------解决方案--------------------
用没用代理?
------解决方案--------------------
探讨
myWebClient.Credentials = new NetworkCredential("X", "X");

------解决方案--------------------
myWebClient.UseDefaultCredentials = true;注释掉看看?
------解决方案--------------------
服务端是不是windows认证啊,是windows认证才可以用NetworkCredential