string Url = "http://xxx.xxx.com ";
Uri uri;
HttpWebRequest req = null;
uri = new Uri(Url);
req = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse res = null;
res = (HttpWebResponse)req.GetResponse();
我用VS2005运行时候最后一句话没问题,但是放到IIS下运行最后一句话就会出问题:(有的Url有问题,有的没有...)
System.Net.WebException: The remote server returned an error: (403) Forbidden. at System.Net.HttpWebRequest.GetResponse() at _Default.Page_Load(Object sender, EventArgs e)
请问是IIS的问题吗?
------解决方案--------------------------------------------------------
不是。是权限的问题,网站要权限才能访问。
设置HttpWebRequest.Credentials; 如果还有问题,抓一下http的包,看它的格式。
按如下方法发送:
CookieContainer cookieBox = new CookieContainer();
string data = "xxxxxxx ";
HttpWebRequest hWreq = (HttpWebRequest)WebRequest.Create( "http://xxx.com ");
hWreq.ContentType = "application/x-www-form-urlencoded ";
hWreq.Method = "POST ";
hWreq.CookieContainer = cookieBox;
hWreq.AllowAutoRedirect = true;
hWreq.ContentLength = data.Length;
Stream stream = hWreq.GetRequestStream();
StreamWriter sw = new StreamWriter(stream);
sw.Write(data);
sw.Close();
stream.Close();
//post数据后,再取HttpWebResponse
HttpWebResponse hWresp = (HttpWebResponse)hWreq.GetResponse();
hWresp.Cookies = cookieBox.GetCookies(hWreq.RequestUri);
Stream respStream = hWresp.GetResponseStream();
StreamReader reader = new StreamReader(respStream);
string outdata = reader.ReadToEnd();
reader.Close();
respStream.Close();