在MVC学习系列继续之前,这里先插一篇——HttpWebRequest模拟浏览器的GET、POST请求。利用get 获取某一个电影,然后用POST修改。在这之前先将之前创建的MVC项目发布,如下图:
在模拟请求之前,我们先用Fiddler检测一下GET请求:
现在我们创建一个窗体应用程序,然后利用HttpWebRequest 按照上图中的HTTP头部信息模拟 GET请求,下面是代码:
private void LoadPre()
{
string url = "http://192.168.1.104/Movies/Edit/5";
HttpWebRequest request = null;
request = WebRequest.Create(url) as HttpWebRequest;
request.Accept = "text/html, application/xhtml+xml, image/jxr, */*";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586";
request.Method = "GET";
request.Host = "192.168.1.104";
request.KeepAlive = true;
request.Referer = "http://192.168.1.104/";
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.Headers.Add("Accept-Language", "zh-CN");
//如果没有 下面这行代码 将获取不到相应的Cookie (r