C#如何模拟登陆?

Crytoll 2012-12-03 07:49:42
点击“登录”按钮会执行以下函数:

res= loginapi.login("109066557@qq.com", "123456");

login函数定义如下:

public bool login(string username, string password)
{
public string baseUrl = @"http://api.website.com/";
DataRequest request = new DataRequest();
request.paramData = @"?username="+username+@"&password="+password;
request.RequestURL(baseUrl + "login.php");
int delay = 5;
while (request.statusCode!= HttpStatusCode.OK)
{
Thread.Sleep(1000);
delay--;
if (delay <= 0)
{
MessageBox.Show("超时!");
MessageBox.Show(request.statusCode.ToString());
return false;
}
}
MessageBox.Show(request.resData);
return true;
}

DataRequest类定义如下:

public class DataRequest
{
public string paramData=string.Empty;
public string resData=string.Empty;
public string resData = "测试";
public HttpStatusCode statusCode;
public Exception errorAsync;
public static Encoding dataEncode = Encoding.UTF8;
public void RequestURL(string url)
{
statusCode = HttpStatusCode.Created;
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webReq);
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
byte[] byteArray = dataEncode.GetBytes(paramData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
catch (Exception ee)
{
errorAsync = ee;
}
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
statusCode=response.StatusCode;
resData = streamRead.ReadToEnd();
streamResponse.Close();
streamRead.Close();
}
catch (Exception ee)
{
errorAsync = ee;
}
}
}

可是并没有传值成功,该如何改呢?
...全文
186 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cysolo 2012-12-04
  • 打赏
  • 举报
回复

request.paramData = @"EMAIL="+username+@"&PASSWORD="+password+@"&debug=";
//username是经过url编码的,password是经过md5加密的
Crytoll 2012-12-03
  • 打赏
  • 举报
回复
引用 8 楼 cuit 的回复:
如果header里没有什么特别的说明不是用post传递的。 可以看webform里的键值对。
登陆页面的PHP代码如下:

<form method="post" action="http://api.website.com/login.php">
EMAIL<input type="text" name="EMAIL"  />
PASSWORD<input type="text" name="PASSWORD"/>
Debug<input type="text" name="debug"/>
<input type="submit"  value="Login" />
</form>
应该就是用post传递的啊。点击TextView可以看到 EMAIL=109066557%40qq.com&PASSWORD=e10adc3949ba59abbe56e057f20f883e&debug= 已知登录密码的加密方式是32位MD5小写, 我现在就是想知道request.paramData = @"EMAIL="+username+@"&password="+password这句代码该如何改呢?
gnimgnot 2012-12-03
  • 打赏
  • 举报
回复
如果header里没有什么特别的说明不是用post传递的。 可以看webform里的键值对。
Crytoll 2012-12-03
  • 打赏
  • 举报
回复
点击TextView可以看到 EMAIL=109066557%40qq.com&PASSWORD=e10adc3949ba59abbe56e057f20f883e&debug= 已知登录密码的加密方式是32位MD5小写, request.paramData = @"EMAIL="+username+@"&password="+password;这句代码该如何改呢?
Crytoll 2012-12-03
  • 打赏
  • 举报
回复
引用 5 楼 cuit 的回复:
你可以先看request的header。


看不懂啊,不知道用户名和密码是怎么传的
gnimgnot 2012-12-03
  • 打赏
  • 举报
回复
你可以先看request的header。
Crytoll 2012-12-03
  • 打赏
  • 举报
回复
引用 2 楼 cuit 的回复:
先搞清楚服务器需要什么方式的验证,
可以用fiddler先抓一下正常登陆的情况下header和form是怎么传的参。

我听cuit大神的第一次用fiddler,不太会看,截图如下:

但是看不懂啊。。。。。。
Crytoll 2012-12-03
  • 打赏
  • 举报
回复
引用 1 楼 dengkeyu 的回复:
这个只有针对具体的网站抓包分析了。如cookies 值等
我用fiddler抓包,结果为: This response did not set any cookies. This response did not contain a P3P Header.
gnimgnot 2012-12-03
  • 打赏
  • 举报
回复
先搞清楚服务器需要什么方式的验证, 可以用fiddler先抓一下正常登陆的情况下header和form是怎么传的参。
dengkeyu 2012-12-03
  • 打赏
  • 举报
回复
这个只有针对具体的网站抓包分析了。如cookies 值等

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧