使用 HttpRequest 发起HTTP请求
昨天开始选选修课,要登录学校的选课系统,按照大四、大三、大二的顺序依次开放选课权限。听说有工具可以让低年级学生插队提前选课,网上搜索一下还真找到了,是一个使用Javascript实现的,整个只有一个页面, 我也保持一份:/Files/lulu/xk.rar。大致上就是做了一个简化版本的页面,使用xmlHttpRequest向服务器发送ajax选课请求。这样在所有人都登录抢着选课,让服务器不堪重负无法响应时,减少不必要的页面加载。另外年级开放顺序应该是有漏洞,大概是放在前端校验的,可以很简单的绕过。
JS不太熟悉,发现HttpRequest功能差不多,大概试了下例子如下:
//Get请求方式
private string RequestGet(string Url)
{
string PageStr = string.Empty;//用于存放还回的html
Uri url = new Uri(Url);//Uri类 提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。就是处理url地址
try
{
HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(url);//根据url地址创建HTTpWebRequest对象
参数设置
HttpWebResponse response = (HttpWebResponse)httprequest.GetResponse();//使用HttpWebResponse获取请求的还回值
Stream steam = response.GetResponseStream();//从还回对象中获取数据流
StreamReader reader = new StreamReader(steam, Encoding.GetEncoding("gb2312"));//读取数据Encoding.GetEncoding("gb2312")指编码是gb2312,不让中文会乱码的
PageStr = reader.ReadToEnd();
reader.Close();
}
catch (Exception e)
{
PageStr += e.Message;
}
return PageStr;
}
private string RequestGet(string Url)
{
string PageStr = string.Empty;//用于存放还回的html
Uri url = new Uri(Url);//Uri类 提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。就是处理url地址
try
{
HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(url);//根据url地址创建HTTpWebRequest对象
参数设置
HttpWebResponse response = (HttpWebResponse)httprequest.GetResponse();//使用HttpWebResponse获取请求的还回值
Stream steam = response.GetResponseStream();//从还回对象中获取数据流
StreamReader reader = new StreamReader(steam, Encoding.GetEncoding("gb2312"));//读取数据Encoding.GetEncoding("gb2312")指编码是gb2312,不让中文会乱码的
PageStr = reader.ReadToEnd();
reader.Close();
}
catch (Exception e)
{
PageStr += e.Message;
}
return PageStr;
}
private string RequestPost(string Url,string Context)//两个参数分别是Url地址和Post过去的数据
{
string PageStr=string.Empty;
Uri url = new Uri(Url);
byte[] reqbytes=Encoding.ASCII.GetBytes(Context);
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "post";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = reqbytes.Length;
Stream stm = req.GetRequestStream();
stm.Write(reqbytes, 0, reqbytes.Length);
stm.Close();
HttpWebResponse wr = (HttpWebResponse)req.GetResponse();
Stream stream = wr.GetResponseStream();
StreamReader srd= new StreamReader(stream,Encoding.GetEncoding("gb2312"));
PageStr += srd.ReadToEnd();
stream.Close();
srd.Close();
}
catch (Exception e)
{
PageStr += e.Message;
}
return PageStr;
}
{
string PageStr=string.Empty;
Uri url = new Uri(Url);
byte[] reqbytes=Encoding.ASCII.GetBytes(Context);
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "post";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = reqbytes.Length;
Stream stm = req.GetRequestStream();
stm.Write(reqbytes, 0, reqbytes.Length);
stm.Close();
HttpWebResponse wr = (HttpWebResponse)req.GetResponse();
Stream stream = wr.GetResponseStream();
StreamReader srd= new StreamReader(stream,Encoding.GetEncoding("gb2312"));
PageStr += srd.ReadToEnd();
stream.Close();
srd.Close();
}
catch (Exception e)
{
PageStr += e.Message;
}
return PageStr;
}