zoukankan      html  css  js  c++  java
  • webclient 根据链接地址登陆后获取文件

    public static string DownloadFile(string strSourceUrl, string strDestFolder, string strUserName, string strPassword, string strDomain)
    {
    try
    {
    WebClient webclient = CreateWebClient(strUserName, strPassword, strDomain);

    // 下载
    Byte[] filecontents = webclient.DownloadData(strSourceUrl);

    string strFileName = Path.GetFileName(strSourceUrl);

    // 创建文件
    FileStream fs = new FileStream(strDestFolder + strFileName, FileMode.Create, FileAccess.Write);
    // 写文件
    fs.Write(filecontents, 0, filecontents.Length);

    fs.Close();
    return strFileName;
    }
    catch (Exception ex)
    {

    }

    return null;
    }

    // 创建WebClient
    // 参数:用户名,密码,域(用来登陆SharePoint)
    private static WebClient CreateWebClient(string strUserName, string strPassword, string strDomain)
    {
    WebClient webclient = new WebClient();

    if (String.IsNullOrEmpty(strUserName))
    {
    webclient.UseDefaultCredentials = true;
    }
    else
    {
    NetworkCredential credential = new NetworkCredential(strUserName, strPassword, strDomain);
    webclient.Credentials = credential;
    }
    return webclient;
    }

    为记录,方便查阅

  • 相关阅读:
    QAction使用
    QT学后感
    设置背景图片
    获取句柄的方法总结(尤其是对于dll而言)
    unixbench小试
    Java命令行之jar命令
    Java应用程序安装包制作工具简介
    Eclipse使用ant编译时的乱码问题
    Jboss4中使用配置发送邮件
    Exe4j注册码
  • 原文地址:https://www.cnblogs.com/liubinurl/p/4971133.html
Copyright © 2011-2022 走看看