403 禁止使用 HttpWebRequest 类
各位, 当我尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上述网址 http://www.bijint.com/hokkaido/tokei_images/HHMM.jpg< /a> 从浏览器调用时会出现相同的 403 错误 - 它只是意味着所述资源在 Web 服务器上受到保护,并且必须提供一些凭据才能访问它。您需要获取此信息(需要什么样的凭据),然后更新您的代码以传递相同的凭据。
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.