【问题标题】:403 forbidden with HttpWebRequest Class403 禁止使用 HttpWebRequest 类
【发布时间】:2011-01-06 09:48:28
【问题描述】:

亲爱的, 当我尝试通过 HttpWebRequest 类获取 Web 图像时遇到 403 禁止消息。 我的代码在下面列出。我该如何解决?谢谢!

public void getWebData()
    {
        string url = "http://www.bijint.com/hokkaido/tokei_images/HHMM.jpg";
        /*****  "HH" stands for hour of current time and "MM" for minute  *****/
        HttpWebRequest httpWebRequest = null;
        HttpWebResponse httpWebResponse = null;
        BinaryReader binaryReader = null;
        FileStream outputFile = null;
        BinaryWriter binaryWriter = null;
        StreamReader streamReader = null;

        try
        {
            httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";
            httpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 GTB7.1 ( .NET CLR 3.5.30729)";
            httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

            httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
            string httpContent = streamReader.ReadToEnd();
            listBox1.Items.Add(httpContent);
        }
        catch (WebException wex)
        {
            listBox1.Items.Add("Exception occurred on request: " + wex.Message);
            if (wex.Status == WebExceptionStatus.ProtocolError)
                httpWebResponse = (HttpWebResponse)wex.Response;
        }
        finally
        {
            if (httpWebResponse != null)
                httpWebResponse.Close();
            if (binaryReader != null)
                binaryReader.Close();
            if (streamReader != null)
                streamReader.Close();
            if (outputFile != null)
                outputFile.Close();
            if (binaryWriter != null)
                binaryWriter.Close();
        }
    }

【问题讨论】:

  • VinayC 大部分是正确的,但您必须参考内容提供商来解决(通过允许匿名访问或向您提供可以实施的凭据)。

标签: c# httpwebrequest http-status-code-403


【解决方案1】:

当从浏览器调用时,上述 url http://www.bijint.com/hokkaido/tokei_images/HHMM.jpg 给出相同的 403 错误 - 这仅意味着上述资源在 Web 服务器上是安全的,并且必须提供一些凭据才能访问它。您需要获取此信息(需要什么样的凭据),然后更新您的代码以传递相同的凭据。

【讨论】:

  • 不,您认为 401 - 403 意味着他的凭据不好,尽管当然服务器可能总是歪曲这一点,并且 OPs 代码中缺少凭据表明实际解决方案只是找到并提供明确的凭据。
  • 在firefox中,如果先访问“bijint.com/hokkaido”,则可以访问“bijint.com/hokkaido/tokei_images/HHMM.jpg”。顺便说一下,“HH”代表当前时间的小时,“MM”代表分钟
  • @Daniel - 进行试验,似乎只能通过此 URL 访问当前时间的图像。对于所有其他图像 url,您将获得 403 - 我的猜测是其他图像可能被删除,或者有任何活动脚本正在根据当前时间生成图像。无论哪种情况,您都必须联系内容提供商。
猜你喜欢
  • 2012-07-29
  • 1970-01-01
  • 2011-12-08
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 2017-11-28
  • 1970-01-01
相关资源
最近更新 更多