簡體   English   中英

禁止使用HttpWebRequest類的403

[英]403 forbidden with HttpWebRequest Class

親愛的所有人,當我嘗試通過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();
        }
    }

從瀏覽器調用時,上述網址http://www.bijint.com/hokkaido/tokei_images/HHMM.jpg也會出現403錯誤-這僅表示該資源在Web服務器上是安全的,並且必須提供一些憑據才能訪問它。 您需要獲取此信息(需要哪種憑據),然后更新代碼以傳遞相同的憑據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM