|
[code=csharp]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
HttpWebRequest request;
HttpWebResponse respone;
CookieContainer cc = new CookieContainer();
string content = GetHtml("http://www.sufeinet.com/member.php?mod=logging&action=login");
string formhash = Content(content, "\"formhash\" value=\"", "\" />");//取formhash
string postdata = "formhash=" + formhash + "&referer=http%3A%2F%2Fbbs.fishc.com%2F&loginfield=username&username=" + txtuser.Text + "&password=" + txtpass.Text + "&questionid=0&answer=&cookietime=2592000";
textBox1.Text = content;
textBox2.Text = formhash;
byte[] postdatastr = Encoding.UTF8.GetBytes(postdata);
try
{
request = (HttpWebRequest)WebRequest.Create("http://bbs.fishc.com/member.php?mod=logging&action=login");
request.CookieContainer = cc;
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postdatastr.Length;
Stream stream = request.GetRequestStream();
//发送数据
stream.Write(postdatastr, 0, postdatastr.Length);
stream.Close();
respone = (HttpWebResponse)request.GetResponse();
}
catch
{
}
}
//得到网页源码
public string GetHtml(string url)
{
HttpWebRequest requrest = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse respone = (HttpWebResponse)requrest.GetResponse();
Stream getrespone = respone.GetResponseStream();
StreamReader sr = new StreamReader(getrespone, Encoding.Default);
string html = sr.ReadToEnd();
sr.Close();
getrespone.Close();
return html;
}
//取中间字符
public string Content(string s, string s1, string s2)
{
int n1, n2;
n1 = s.IndexOf(s1, 0) + s1.Length;
n2 = s.IndexOf(s2, n1);
return s.Substring(n1, n2 - n1);
}
}
}[/code]
论坛登录不了,是我少了东西还是?
|
|