private void Get_Request() { #region 构造请求头 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(GetUrl); request.Method = "GET"; SetHeader.SetHeaderValue(request.Headers, "Host", "ojjx.wzu.edu.cn"); SetHeader.SetHeaderValue(request.Headers, "Connection", "keep-alive"); request.Headers["Upgrade-Insecure-Requests"] = "1"; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; request.Headers["DNT"] = "1"; request.Referer = "http://ojjx.wzu.edu.cn/xs_main.aspx?xh=" + LoginInfo.UserAccount; request.Headers["Accept-Encoding"] = "gzip, deflate"; request.Headers["Accept-Language"] = "zh-CN,zh;q=0.8"; request.CookieContainer = LoginInfo.EACookie; #endregion #region 返回并接收数据 try { HttpWebResponse res = (HttpWebResponse)request.GetResponse(); StreamReader ReaderOfStream = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312")); Output = ReaderOfStream.ReadToEnd(); Status = true; } catch (Exception e) { Status = false; LoginInfo.FailedReason = "GET请求出现异常,请检查网络是否中断或者丢包。"; LoginInfo.FailedLog = e.Message; } #endregion }
protected void Post_Request() { #region 构造请求头 HttpWebRequest HttpRequest = (HttpWebRequest)HttpWebRequest.Create(PostUrl); HttpRequest.Method = "POST"; SetHeader.SetHeaderValue(HttpRequest.Headers, "Host", "ojjx.wzu.edu.cn"); SetHeader.SetHeaderValue(HttpRequest.Headers, "Connection", "keep-alive"); HttpRequest.Headers["Cache-Control"] = "max-age=0"; HttpRequest.Headers["Origin"] = "http://ojjx.wzu.edu.cn"; HttpRequest.Headers["Upgrade-Insecure-Requests"] = "1"; HttpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"; HttpRequest.ContentType = "application/x-www-form-urlencoded"; HttpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; HttpRequest.Headers["DNT"] = "1"; HttpRequest.Referer = ReferUrl; HttpRequest.Headers["Accept-Encoding"] = "gzip, deflate"; HttpRequest.Headers["Accept-Language"] = "zh-CN,zh;q=0.8"; HttpRequest.CookieContainer = LoginInfo.EACookie; #endregion #region 创建IO流写入POST内容 Stream stream = HttpRequest.GetRequestStream(); stream.Write(Bytesarray, 0, Bytesarray.Length); stream.Close(); #endregion #region 返回并接收数据 try { HttpWebResponse httpResponse = (HttpWebResponse)HttpRequest.GetResponse(); StreamReader ReadersOfStream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312")); Output = ReadersOfStream.ReadToEnd(); Status = true; } catch (Exception e) { Status = false; LoginInfo.FailedReason = "Post请求异常,请检查网络是否中断或者丢包。"; LoginInfo.FailedLog = e.Message; } #endregion }