403 禁止使用 HttpWebRequest 类

发布于 2024-10-10 23:39:59 字数 1927 浏览 4 评论 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();
        }
    }

Dear all,
I encountered 403 forbidden message when I try to get web image by HttpWebRequest Class.
my code is listed below. How can I solve it ? Thanks !

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();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-10-17 23:39:59

The said url http://www.bijint.com/hokkaido/tokei_images/HHMM.jpg gives same 403 error when invoked from browser - it simply means that the said resource is secured on the web server and one must supply some credentials to access it. You need to get this information (what kind of credentials are required) and then update your code to pass same credentials.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文