【问题标题】:C# download image from src without extensionC#从src下载图片,不带扩展名
【发布时间】:2017-09-03 08:50:43
【问题描述】:

我想从这个网址http://squirlytraffic.com/surficon.php?ts=1491591235下载一张图片

我尝试了此代码,但打开它时看不到图像。

using (WebClient client = new WebClient())
  {
    client.DownloadFile("http://squirlytraffic.com/surficon.php?ts=1491591235", @"D:\image.jpg");         
  }

【问题讨论】:

  • url重定向登录,下载图片需要先认证
  • 我已在网络浏览器中登录。
  • 不,您必须从 Web 客户端执行此操作

标签: c# image url download


【解决方案1】:

您需要使用 WebClient Credentials 属性设置您的凭据。您可以通过为其分配NetworkCredential 的实例来完成此操作。见下文:

using (WebClient client = new WebClient()){
  client.Credentials = new NetworkCredential("user-name", "password");
  client.DownloadFile("url", @"file-location");
}

编辑

如果您不想硬编码用户名和密码,可以将 WebClient 的 UseDefaultCredentials 属性设置为 true。这将使用当前登录用户的凭据。来自documentation。

Credentials 属性包含用于访问主机上资源的身份验证凭据。在大多数客户端场景中,您应该使用 DefaultCredentials,它是当前登录用户的凭据。为此,请将 UseDefaultCredentials 属性设置为 true,而不是设置此属性。

这意味着您可以将上述代码修改为:

using (WebClient client = new WebClient()){
  client.UseDefaultCredentials = true;
  client.DownloadFile("url", @"file-location");
}

【讨论】:

    【解决方案2】:

    试试这个方法,登录的时候传那些参数

                StringBuilder postData = new StringBuilder();
                postData.Append("login=" + HttpUtility.UrlEncode("username") + "&");
                postData.Append("password=" + HttpUtility.UrlEncode("password") + "&");
                postData.Append("Submit=" + HttpUtility.UrlEncode("Login"));
                ASCIIEncoding ascii = new ASCIIEncoding();
                byte[] postBytes = ascii.GetBytes(postData.ToString());
                CookieContainer cc = new CookieContainer();
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create("http://squirlytraffic.com/members.php");
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.ContentLength = postBytes.Length;
                webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                webReq.CookieContainer = cc;
                Stream postStream = webReq.GetRequestStream();
                postStream.Write(postBytes, 0, postBytes.Length);
                postStream.Flush();
                postStream.Close();
                HttpWebResponse res = (HttpWebResponse)webReq.GetResponse();
                HttpWebRequest ImgReq = (HttpWebRequest)WebRequest.Create("http://squirlytraffic.com/surficon.php?ts=1491591235");
                ImgReq.Method = "GET";
                ImgReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                ImgReq.CookieContainer = cc;
                HttpWebResponse ImgRes = (HttpWebResponse)ImgReq.GetResponse();
                Stream Img = ImgRes.GetResponseStream();
    

    【讨论】:

    • 不工作。如何检查我是否使用 webrequest 登录?
    • 如果此代码下载图像,我在哪里可以找到驱动器中的图像?
    • 登录后响应 HTML 将显示登录后页面,上面的代码只是检索流,您需要将该流写入文件
    猜你喜欢
    • 1970-01-01
    • 2018-03-07
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2012-10-17
    • 2022-01-05
    相关资源
    最近更新 更多