HttpWebResponse 远程服务器返回错误: (403) 已禁止
前几天以下代码有个小问题:
GET形势获取URL的返回值出现对访问同一URL过于频繁就会出现403已禁止
1 string result = ""; 2 HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); 3 HttpWebResponse respone = (HttpWebResponse)httpRequest.GetResponse(); 4 StreamReader stream = new StreamReader(respone.GetResponseStream(), Encoding.UTF8); 5 result = stream.ReadToEnd();
解决方案加个获取身份验证信息
1 string result = ""; 2 HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl); 3 httpRequest.Timeout = 10000; 4 httpRequest.UserAgent = "Code Sample Web Client"; 5 httpRequest.Credentials = CredentialCache.DefaultCredentials; 6 HttpWebResponse respone = (HttpWebResponse)httpRequest.GetResponse(); 7 StreamReader stream = new StreamReader(respone.GetResponseStream(), Encoding.UTF8); 8 result = stream.ReadToEnd();