HttpClient PostAsync
2016-04-28 18:59
148 查看
作者:刘杰
链接:http://www.zhihu.com/question/28050747/answer/41181841
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
String result = ""; HttpResponseMessage response = new HttpResponseMessage(); HttpClient httpClient = new HttpClient(); //登陆页面网址 String urlLogin = "https://ids-swust.fayea.com/cas/login?service=https%3A%2F%2Fmatrix%2Edean%2Eswust%2Eedu%2Ecn%2FacadmicManager%2Findex%2Ecfm%3Fevent%3DstudentPortal%3ADEFAULT%5FEVENT";
//登陆成功页面(倒计时,未真正进入主页) String urlLogined = "https://matrix.dean.swust.edu.cn/acadmicManager/index.cfm?event=studentPortal:DEFAULT_EVENT/"; //主页,需加上从上一个网页获取的ticket String urlMainPage = "https://matrix.dean.swust.edu.cn/acadmicManager/index.cfm?event=studentPortal:DEFAULT_EVENT&ticket=";
//课表页面 String urlTable = "https://matrix.dean.swust.edu.cn/acadmicManager/index.cfm?event=studentPortal:courseTable"; //设置 httpClient.MaxResponseContentBufferSize = 256000; httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"); //进入登录页面(这一步是不是必须的安?) response = await httpClient.GetAsync(new Uri(urlLogin)); //result = response.Content.ReadAsStringAsync().Result; //要post去的信息 List<KeyValuePair<String, String>>
paramList = new List<KeyValuePair<String, String>>(); paramList.Add(new KeyValuePair<string, string>("lt", "LT-F11BE3E7-DC17-B420-8430351ECF173F51")); paramList.Add(new KeyValuePair<string, string>("username", username)); paramList.Add(new KeyValuePair<string,
string>("password", password)); paramList.Add(new KeyValuePair<string, string>("service", urlLogined)); //post过去,得到倒计时的页面 response = await httpClient.PostAsync(new Uri(urlLogin), new FormUrlEncodedContent(paramList)); result = response.Content.ReadAsStringAsync().Result;
//把得到的ticket加上再get几次,因为有倒计时,只好循环get直到内容变化,证明成功进入主页为止 int length = 0; for (int i = 0; i<10; i++)// { response = await httpClient.GetAsync(new Uri(urlMainPage)+GetTicket(result));//urlAim + GetTicket(result) result = response.Content.ReadAsStringAsync().Result;
if (i == 0)//记录第一次得到的源码长度,肯定没有成功进入 length = result.Length; if (i > 0 && result.Length != length) break;//登陆成功 } //进入课表页面 response = await httpClient.GetAsync(new Uri(urlTable)); result = response.Content.ReadAsStringAsync().Result; httpClient.Dispose();
链接:http://www.zhihu.com/question/28050747/answer/41181841
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
String result = ""; HttpResponseMessage response = new HttpResponseMessage(); HttpClient httpClient = new HttpClient(); //登陆页面网址 String urlLogin = "https://ids-swust.fayea.com/cas/login?service=https%3A%2F%2Fmatrix%2Edean%2Eswust%2Eedu%2Ecn%2FacadmicManager%2Findex%2Ecfm%3Fevent%3DstudentPortal%3ADEFAULT%5FEVENT";
//登陆成功页面(倒计时,未真正进入主页) String urlLogined = "https://matrix.dean.swust.edu.cn/acadmicManager/index.cfm?event=studentPortal:DEFAULT_EVENT/"; //主页,需加上从上一个网页获取的ticket String urlMainPage = "https://matrix.dean.swust.edu.cn/acadmicManager/index.cfm?event=studentPortal:DEFAULT_EVENT&ticket=";
//课表页面 String urlTable = "https://matrix.dean.swust.edu.cn/acadmicManager/index.cfm?event=studentPortal:courseTable"; //设置 httpClient.MaxResponseContentBufferSize = 256000; httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"); //进入登录页面(这一步是不是必须的安?) response = await httpClient.GetAsync(new Uri(urlLogin)); //result = response.Content.ReadAsStringAsync().Result; //要post去的信息 List<KeyValuePair<String, String>>
paramList = new List<KeyValuePair<String, String>>(); paramList.Add(new KeyValuePair<string, string>("lt", "LT-F11BE3E7-DC17-B420-8430351ECF173F51")); paramList.Add(new KeyValuePair<string, string>("username", username)); paramList.Add(new KeyValuePair<string,
string>("password", password)); paramList.Add(new KeyValuePair<string, string>("service", urlLogined)); //post过去,得到倒计时的页面 response = await httpClient.PostAsync(new Uri(urlLogin), new FormUrlEncodedContent(paramList)); result = response.Content.ReadAsStringAsync().Result;
//把得到的ticket加上再get几次,因为有倒计时,只好循环get直到内容变化,证明成功进入主页为止 int length = 0; for (int i = 0; i<10; i++)// { response = await httpClient.GetAsync(new Uri(urlMainPage)+GetTicket(result));//urlAim + GetTicket(result) result = response.Content.ReadAsStringAsync().Result;
if (i == 0)//记录第一次得到的源码长度,肯定没有成功进入 length = result.Length; if (i > 0 && result.Length != length) break;//登陆成功 } //进入课表页面 response = await httpClient.GetAsync(new Uri(urlTable)); result = response.Content.ReadAsStringAsync().Result; httpClient.Dispose();
相关文章推荐
- 7.让网站支持http和https的访问方式
- 北京林业大学“计蒜客”杯程序设计竞赛 网络赛 D-A letter from Chensg【最长公共子序列LCS】
- 北京林业大学“计蒜客”杯程序设计竞赛 网络赛 C-Candy【模拟】
- linux socket编程 TCP和UDP实例
- URL、URLConnection 和HttpURLConnection
- HttpClient与httpComponents、HttpPost、HttpGet
- TCP/IP协议族各层的协议汇总
- 网络爬虫 -- 批量下载网站图片
- https客户端证书.p12maven打包后tomcat启动不正确
- 用平常语言介绍神经网络(Neural Networks in Plain English)
- 在Asp.net MVC 4.0中使用IHttpHandler
- quick-cocos做客户端,golang做服务端,实现HTTP通信
- HttpClient介绍
- 安装protocolbuffer详解 原创: http://cq520.iteye.com/blog/2024739
- 个人电脑如何搭建服务器?或远程连接
- 机器学习算法----贝叶斯网络
- 图解HTTPS
- MicroERP如何配置网络应用
- MicroERP如何配置网络应用
- 解决阿里云VPC网络下面ECS不能指定高于X.X.X.247私有IP地址的方法