WebRequest问题?
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
nameSpace TestIMDb{
Class Class1{
static void Main(string[] args)
{
string strImdbResult = null;
Encoding strEncode = System.Text.Encoding.GetEncoding("UTF-8");
try
{
WebRequest wreq = WebRequest.Create("http://www.imdb.com");
WebResponse wres = wreq.GetResponse();
StreamReader sr = new StreamReader(wres.GetResponseStream(), strEncode);
strImdbResult = sr.ReadToEnd();
string strFile = "result.html";
StreamWriter sw = File.CreateText(strFile);
sw.Write(strImdbResult);
sw.WriteLine();
sw.Close();
DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
string strFilePath = di.FullName + "\\" + strFile;
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "iexplore.exe";
myProcess.StartInfo.Arguments = strFilePath;
myProcess.Start();
}
catch( WebException e )
{
Console.WriteLine( "IMDB :" + e.Message );
}
catch( Exception e )
{
Console.WriteLine( "IMDB :" + e.Message );
}
}
}
}
请各位测试一下这段代码,我总是得到403 Fobidden的异常,而只要把www.imdb.com改为其他的网址如www.google.com等就可以,不知道这是为什么?应该不是imdb网站的问题,因为我朋友用java可以对它做同样的操作。