如何获取post提交后的响应正文?
像这样的一段代码{"code":true,"homeUrl":"http://www.renren.com/callback.do?t=a6ca5897e4096f4f12e04bf494db56460&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&needNotify=false"};我用的是httpwebrequest类 --------------------编程问答-------------------- request.form[""] 你是怎么发送的 --------------------编程问答-------------------- post发送的啊。就是这个响应应该用什么参数返回呢? --------------------编程问答-------------------- 你可以先看下这个http://topic.csdn.net/u/20120407/14/b814937f-7e25-4a91-83ed-f720e52b3ac1.html --------------------编程问答-------------------- msdnsearch
webrequest
--------------------编程问答-------------------- 什么意思啊? --------------------编程问答-------------------- WebRequest.GetResponse --------------------编程问答-------------------- 这返回的是html源码吧。我想要的不是这个,看标题 --------------------编程问答--------------------
你所谓的正文是什么?
--------------------编程问答-------------------- 如果是js处理后的结果,用webbrower
你了解一下什么是bs程序就清楚了 --------------------编程问答-------------------- {"code":true,"homeUrl":"http://www.renren.com/callback.do?t=35466507bb49807292e00bbe5063b9090&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&needNotify=false"} 就是这样的一段代码,我是新手,也不是很懂啊。应该用什么参数返回呢? --------------------编程问答--------------------
--------------------编程问答-------------------- 你这个判断登录是否成功的不具有普遍性啊,因为返回的网页不一定包含登录成功信息,有没有什么更好的判断条件呢?
// 设置打开页面的参数
URI = "POST提交的URL";
request = WebRequest.Create(URI) as HttpWebRequest;
request.Method = "GET";
request.KeepAlive = false;
// 接收返回的页面
/*针对ASP.NET网站
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
string srcString = reader.ReadToEnd();
// 获取页面的 VeiwState
string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
int j = srcString.IndexOf("\"", i);
string viewState = srcString.Substring(i, j - i);
// 获取页面的 EventValidation
string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
j = srcString.IndexOf("\"", i);
string eventValidation = srcString.Substring(i, j - i);
*/
///////////////////////////////////////////////////
// 提交按钮的文本
string submitButton = "%E7%99%BB%E5%BD%95";
// 用户名和密码
string userName = "qq123456";
string password = "123456";
// 将文本转换成 URL 编码字符串
viewState = System.Web.HttpUtility.UrlEncode(viewState);
eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
// 要提交的字符串数据。格式形如:user=uesr1&password=123
string formatString = "userid={0}&password={1}&txtAccount={2}&__VIEWSTATE={3}";
string postString = string.Format(formatString, userName, password, submitButton, viewState);
// 将提交的字符串数据转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
// 设置提交的相关参数
request = WebRequest.Create(URI) as HttpWebRequest;
request.Method = "POST";
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET4.0C; .NET4.0E)";
request.CookieContainer = cookieContainer;
request.ContentLength = postData.Length;
// 提交请求数据
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
// 接收返回的页面
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
srcString = reader.ReadToEnd();
if (srcString.Contains("登录失败"))
{
MessageBox.Show("登录失败!");
}
else
{
MessageBox.Show("登录成功!");
button2.Enabled = false;
}
补充:.NET技术 , C#