请教个问题,关于WebClient post请求的问题

caicaihui 2013-02-28 02:13:12
WebClient发送的时候出现个问题,如果正确的帐号放在第一个是能匹配正确的,但是放入到第二个就会匹配错误,找了半天不太清楚是什么问题。帮忙看看。


private void button1_Click(object sender, EventArgs e)
{
string url = "http://xc.netease.com/logging.php?action=login";

string path = Environment.CurrentDirectory + @"\list.txt";
string[] list = System.IO.File.ReadAllText(path).Split('\r');
Application.DoEvents();
for (int i = 0; i < list.Length; i++)
{
string[] arr = list[i].Split('-');
this.richTextBox1.Text += String.Format("{0}-{1} {2}", arr[0].Trim(), arr[1].Trim(), loginpage(url, arr[0], arr[1])) + Environment.NewLine;
this.Refresh(1);
}
this.richTextBox1.Text += "完成" + Environment.NewLine;
}


public string loginpage(string url, string uid, string password)
{
try
{
WebClient wc = new WebClient();
wc.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0");
string downhtml = System.Text.Encoding.GetEncoding("gb2312").GetString(wc.DownloadData(url));

string formhash = System.Text.RegularExpressions.Regex.Replace(downhtml, "[\\s*\\S*]*<input type=\"hidden\" name=\"formhash\" value=\"([^\"]*)\"[\\s*\\S*]*", "$1", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string poststring = String.Format("formhash={0}&referer=index.php&loginfield=uid&username={1}&password={2}&questionid=0&answer=&cookietime=2592000&loginmode=&styleid=&loginsubmit=%CC%E1%BD%BB", formhash, uid, password);

wc = new WebClient();
wc.Headers.Add("Accept-Language", "zh-cn");
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

byte[] responseData = wc.UploadData(url, "POST", System.Text.Encoding.Default.GetBytes(poststring));
wc.Dispose();

string returnhtml = System.Text.Encoding.GetEncoding("gb2312").GetString(responseData);
int s_p = returnhtml.IndexOf("<p>");
int l_p = returnhtml.IndexOf("</p>");

return returnhtml.Substring(s_p + 3, l_p - s_p - 3);
}
catch
{
return String.Empty;
}
}
public void Refresh(int s)
{
for (int i = 0; i < s*10; i++)
{
Application.DoEvents();
Thread.Sleep(100);
}
}


list.txt 文件,下面的70282是正确的

221-3
70282-1
...全文
109 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
md5e 2013-02-28
  • 打赏
  • 举报
回复
看看是否有多余的字符 最好.Trim()一下

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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