c#模拟网页自动登录的一段代码,遇到了问题
using System;using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
namespace MyLogin
{
class PostGetWebRequest
{
public string LoginUrl;
// private CookieContainer MyCookieContainer;
private HttpWebRequest request;
private HttpWebResponse response;
private Stream stream;
private StreamReader streamReader;
private string Src = "";
private string postData;
public string HtmlUserName;
public string HtmlPassword;
// private string HtmlSubmit;
public string PostWebRequest()
{
request = (HttpWebRequest)WebRequest.Create(LoginUrl);
MyCookieContainer = new CookieContainer();
//设置提交相关参数
postData = "email=" + HtmlUserName.Replace("@", "%40") + "&password=" + HtmlPassword + "&origURL=" + LocationUrl + "&domain=renren.com";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.KeepAlive = false;
request.CookieContainer = MyCookieContainer;//把取服务器发给客户端跟session 想对应的cookie;相当于是一个容器
//提交请求
stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
//接收
response = (HttpWebResponse)request.GetResponse();
streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
Src = streamReader.ReadToEnd();
streamReader.Close();
stream.Close();
return Src;
}
}
class Program
{
static void Main(string[] args)
{
PostGetWebRequest postGetWebRequest = new PostGetWebRequest();// 没有验证码的网站,登录页面html格式,客户端的
postGetWebRequest.HtmlUserName = "xxx@163.com";
postGetWebRequest.HtmlPassword = "aaaaaa";
postGetWebRequest.LoginUrl = "http://www.renren.com/PLogin.do";//这个是人人的登录页
string content1 = postGetWebRequest.PostWebRequest();
Console.WriteLine( content1);
Console.ReadKey();
}
}
}
问题是我想获得post后响应的正文比如 {"code":true,"homeUrl":"http://www.renren.com/callback.do?t=35466507bb49807292e00bbe5063b9090&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&needNotify=false"}
我该怎么修改代码?? --------------------编程问答-------------------- 什么意思,content1 不是返回的内容吗,如果想post其他地址,就修改url那个网站需要提交的参数,每个页面的参数可能不一样,你先抓包看看都提交了哪些,自己对着组织 --------------------编程问答-------------------- 我运行后返回了一段登录前页面的源代码,但我无法判断是否登录成功啊,如果我能得到post的响应正文就可以,问题是怎么获得? --------------------编程问答-------------------- {"code":true,"homeUrl":"http://www.renren.com/callback.do?t=35466507bb49807292e00bbe5063b9090&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&needNotify=false"}这是登录成功的
{"catchaCount":1,"code":false,"homeUrl":"http://www.renren.com/SysHome.do?origURL=http%3A%2F%2Fwww.renren.com%2Fhome&catchaCount=1&failCode=128","failDescription":"您的用户名和密码不匹配","failCode":128,"email":"18986143802"}这是登录失败的
我该怎么获取这段代码?
补充:.NET技术 , C#