【问题标题】:How to access secure url using webclient如何使用 webclient 访问安全 url
【发布时间】:2023-03-16 16:41:02
【问题描述】:

我正在尝试通过 Web 客户端访问安全 URL,但几乎没有错误。 总体目的是从安全 url 下载图像..

请注意,之前,我可以使用 http 从存储库下载图像,现在他们已在同一 url 中强制执行(安全)https。 我们有一个控制台应用程序,它定期运行并从服务器拉取图像并在本地存储。

请告知我做错了什么?还是缺少什​​么?

以下是我面临的问题。

  1. /// 如果我使用代码部分 1... 我收到错误消息说参数无效。
  2. /// 如果我使用代码部分 2... 文件保存到本地,但收到一条消息“文件似乎已损坏或过大且无法打开。

下面列出了第 1 节和第 2 节。

            using (WebClient webClient = new WebClient())
            {

                webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                webClient.Credentials = new NetworkCredential("id", "pwd");  // if credentials are wrong...still I get the imageByte

                /// section 1 - if I use this code... I get error saying Parameter is not valid.
                // ----------------------------- Start --------------------------------
                imageByte = webClient.DownloadData("https://someurl/source/abcd.jpeg");
                ////Initialize image variable
                Image newImage;
                ////Read image data into a memory stream
                using (MemoryStream ms = new MemoryStream(imageByte, 0, imageByte.Length, true))
                {
                    ms.Write(imageByte, 0, imageByte.Length);

                    //Set image variable value using memory stream.
                    newImage = Image.FromStream(ms, true, false); // Throws error at this line saying that parameter is not valid.
                }
                newImage.Save(@"location\image.jpeg");
                // ----------------------------- End --------------------------------


                /// section 2 - if I use this code... File gets saved to local but get a message " File appear to be corrupt or large and not able to open.
                // ----------------------------- Start --------------------------------
                webClient.DownloadFile("https://someurl/source/abcd.jpeg", @"location\image.jpeg");
                // ----------------------------- End --------------------------------

            } 

【问题讨论】:

    标签: c# webclient webclient-download


    【解决方案1】:

    这两个错误都表明下载的数据不是有效的 JPEG 图像。可能是因为服务器返回了一些错误。

    为了检查服务器是否返回错误,您可以查看 HTTP 响应标头和正文中的内容。

    • 对于正文,您可以尝试使用文本编辑器打开第 2 节中保存的文件。

    • 对于标头,您可以使用调试器或对您的程序进行简单更新来检查 webClient.ResponseHeaders property

    以下示例取自MSDN

    // Obtain the WebHeaderCollection instance containing the header name/value pair from the response.
    WebHeaderCollection myWebHeaderCollection = myWebClient.ResponseHeaders;
    
    Console.WriteLine("\nDisplaying the response headers\n");
    
    // Loop through the ResponseHeaders and display the header name/value pairs.
    for (int i=0; i < myWebHeaderCollection.Count; i++)
    {       
        Console.WriteLine ("\t" + myWebHeaderCollection.GetKey(i) + " = " + myWebHeaderCollection.Get(i));
    }
    

    查看服务器实际返回内容的另一种方法是Invoke-WebRequest PowerShell cmdletwget utility(都应支持 HTTPS 和身份验证)

    【讨论】:

    • 感谢您的意见。我尝试从响应头中获取信息,但没有看到任何错误。我得到以下回应。 Pragma = no-cache Cache-Control = no-cache, no-store Expires = Thu, 07, 2016 00:00:00 GMT Set-Cookie = JSESSIONID = "some GUID value"; Path=/abcd/;Secure;HttpOnly Server = Apache Content-Type= text/html;charset=UTF-8 Content-Language=en-US Transfer-Encoding=Chunked Vary=Accept-Encoding Date=Fri,12,2016 什么我可以看到过期设置为昨天的日期......这可能是一个问题吗?
    • 看起来可疑的是 Content-Type= text/html - 我希望它是 image/jpeg。您是否尝试使用文本编辑器打开保存的文件(响应正文应为 UTF-8 文本)?
    • 你是对的......当我在文本编辑器中打开文件时......它就像魅力一样......这就是我无法在任何图像编辑器中打开文件的原因......所以这里的罪魁祸首是 Content-Type =text/html ...知道我们如何将类型设为 Image 吗?
    • 嗯,您能够使用文本编辑器打开文件这一事实并不意味着什么——您几乎可以在文本编辑器中打开任何可读文件。我的问题主要是关于这个文件的内容 - 有没有关于文件中实际错误的提示?像“找不到文件”、“服务器错误”或类似的东西
    • 不,没有关于错误的提示。该文件包含我请求的 URL 的完整 HTML.. 它包含 html、head、script、body、div ....页面的所有标签..所以当我将安全 url 输入浏览器时.. . 它带我进入一个安全的登录页面... 如果我传递我的 id、pwd .. 并单击登录... 我可以在该安全系统中看到图像... 以及我保存的文件.. . 是页面的完整html内容。
    【解决方案2】:

    您的代码工作正常。 正如 Kel 建议的那样,服务器响应标头很可能给出了答案。您确定服务器配置为支持 https 吗?

    无需提供网络凭据。因为 HTTPS 只是一种传输协议。它只保护连接。

    要进行身份验证,您必须配置服务器,例如进行 BASIC 或 DIGEST 身份验证。如果需要,对请求的资源进行设置授权。

    对你的代码有一个评论:不要忘记处理 newImage。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 2013-02-15
      相关资源
      最近更新 更多