想使用HttpWebRequest自动登录网站,总是遭遇403访问禁止问题
ServicePointManager.MaxServicePoints = 4;
ServicePointManager.MaxServicePointIdleTime = 1000;
Uri MS = new Uri("http://www.szjs.com.cn/");
ServicePoint servicePoint = ServicePointManager.FindServicePoint(MS);
int hashCode=servicePoint.GetHashCode();
try
{
System.Net.HttpWebRequest request=(HttpWebRequest) WebRequest.Create(new Uri("http://www.szjs.com.cn"));
hashCode=request.ServicePoint.GetHashCode();
HttpWebResponse response=(HttpWebResponse)request.GetResponse();
request=(HttpWebRequest) WebRequest.Create(new Uri("http://www.szjs.com.cn/login.asp"));
hashCode=request.ServicePoint.GetHashCode();
request.UserAgent = "Mozilla/4.0";
request.AllowAutoRedirect = true;
request.ContentType="application/x-www-form-urlencoded";
request.Method="post";
string postData="userid=Kzhyjy&pass=3328287&usersort=unit";
ASCIIEncoding encoding=new ASCIIEncoding();
byte[] byte1=encoding.GetBytes(postData);
request.ContentLength=postData.Length;
Stream newStream=request.GetRequestStream();//运行到这一句便出现访问禁止异常,不过有时候又可以过去。
newStream.Write(byte1,0,byte1.Length);
newStream.Close();
response=(HttpWebResponse)request.GetResponse();//同样到这里也出现一样的情况。
System.IO.Stream resstream=response.GetResponseStream();
Byte[] recvBytes=new Byte[response.ContentLength];
resstream.Read(recvBytes,0,(int)response.ContentLength);
string s=System.Text.Encoding.Default.GetString(recvBytes);
string ss=s;
}
catch(WebException Err)
{
string ss=Err.Message+";"+Err.Status;
WebHeaderCollection headers=((HttpWebResponse)Err.Response).Headers;
for(int i=0;i<headers.Count;i++)
{
string[] sss=headers.GetValues(i);
for(int j=0;j<sss.Length;j++)
{
string ssss=sss[j];
}
}
}
遇到类似问题的前辈们盼望能不惜指点一二,怎么解决这个问题,还是想其他办法?
搜索更多相关主题的帖子:
自动 遭遇 访问 登录