0

亲爱的,当我尝试通过 HttpWebRequest 类获取网页图像时遇到 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();
        }
    }
4

1 回答 1

1

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

于 2011-01-06T10:30:44.840 回答