问题描述
- HttpWebRequest能模拟XMLHttpRequest的post请求吗?
-
如果能模拟,请问该如何具体实现。HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(loginpostData); myRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"; myRequest.Accept = "application/json, text/javascript, */*; q=0.01"; myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.Referer = "http://www.ewt360.com/"; myRequest.Host = "www.ewt360.com"; myRequest.ContentLength = data.Length; Stream newStream = myRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); myRequest.CookieContainer = new CookieContainer(); //将请求的结果发送给客户端(界面、应用) HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); myResponse.Cookies = myRequest.CookieContainer.GetCookies(myRequest.RequestUri); cookie.Add(myResponse.Cookies); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); return reader.ReadToEnd();
我的代码在获取响应的时候报错了,我想在一个网站上模拟登陆,发现它登陆的时候发送了三个XHR请求,请问我是不是这三个请求都要模拟一遍,还是只要模拟第一个请求呢?没有C币了真心求好心人帮助,做这个数据抓取我已经做的心力憔悴了
解决方案
肯定可以,如果无法登陆,说明你缺少提交的数据,或者没有找到验证数据的接口地址。
发送3个xhr请求,你得研究下他们是否有关系,如果有关系,你必须3个xhr请求的地址都要用webrequest对象请求一遍,获取需要的数据
时间: 2024-11-01 06:07:40