我现在想通过C# 的post方法来登陆 phpwind 的论坛 并发帖,现在的问题是登陆可以做到,但是发帖部分存在问题。我登陆之后记录下cookie ,然后再用记录下的cookie去“浏览”要发的帖子。结果浏览到的是“未登录状态”。。怎么回事。我的思路有问题吗。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;namespace 威海bbs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} HttpWebRequest httpWebRequest;
HttpWebResponse httpWebResponse;
string responseFromServer; //SendLogin返回的响应流
CookieCollection gCookieCollention; //cookie
string username=""; //用户名
string password=""; //密码 private void button1_Click(object sender, EventArgs e) //登陆网站
{
username = txt_username.Text;
password = txt_password.Text;
if (username != "" && password != "")
{
string PostData = string.Format("ajax=1&jumpurl=http://club.iweihai.cn/&lgt=0&pwpwd={0}&pwuser={1}&step=2",password,username);
string LoginUrl = "http://club.iweihai.cn/login.php?nowtime=1359082517955&verify=e46660bb";
SendLogin(LoginUrl, PostData);
}
else
MessageBox.Show("请输入用户名及密码。。");
} public void SendLogin(string loginUrl, string postData)
{
byte[] byteArray = Encoding.GetEncoding("GBK").GetBytes(postData);
try
{
//基于apache服务器,IIS发布的则不需要
ServicePointManager.Expect100Continue = false;
CookieContainer cookieContainer = new CookieContainer();
//创建对url的请求
httpWebRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/QVOD, application/QVOD, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-xpsdocument, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpWebRequest.Headers["Accept-Language"] = "zh-cn";
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//协议方式
httpWebRequest.Method = "POST";
//post开始
//请求内容长度
httpWebRequest.ContentLength = byteArray.Length;
Stream dataStream = httpWebRequest.GetRequestStream();
// 请求数据放入请求流
dataStream.Write(byteArray, 0, byteArray.Length); //返回html
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("gb2312"));
//读取响应流
responseFromServer = reader.ReadToEnd();
if (responseFromServer.Contains("success"))
{
MessageBox.Show("登陆成功!!");
gCookieCollention = httpWebResponse.Cookies; //保存登陆成功后的cookie
}
else
MessageBox.Show("用户名或密码错误。");
MessageBox.Show(responseFromServer); //读响应流html
reader.Close();
dataStream.Close();
httpWebResponse.Close();
}
} catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} private void btn_Reply_Click(object sender, EventArgs e)
{
VisitUrl(txt_url.Text); //这一步还是未登录状态
}
private void VisitUrl(string url) //转到帖子所在界面
{
CookieContainer cc = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.CookieContainer = cc;
request.KeepAlive = true;
request.CookieContainer.Add(gCookieCollention);
HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(reponse.GetResponseStream(), Encoding.GetEncoding("gb2312"));
string s = sr.ReadToEnd();
txt_Msg.Text = s;
}
}
}
c#.netwindows
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;namespace 威海bbs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} HttpWebRequest httpWebRequest;
HttpWebResponse httpWebResponse;
string responseFromServer; //SendLogin返回的响应流
CookieCollection gCookieCollention; //cookie
string username=""; //用户名
string password=""; //密码 private void button1_Click(object sender, EventArgs e) //登陆网站
{
username = txt_username.Text;
password = txt_password.Text;
if (username != "" && password != "")
{
string PostData = string.Format("ajax=1&jumpurl=http://club.iweihai.cn/&lgt=0&pwpwd={0}&pwuser={1}&step=2",password,username);
string LoginUrl = "http://club.iweihai.cn/login.php?nowtime=1359082517955&verify=e46660bb";
SendLogin(LoginUrl, PostData);
}
else
MessageBox.Show("请输入用户名及密码。。");
} public void SendLogin(string loginUrl, string postData)
{
byte[] byteArray = Encoding.GetEncoding("GBK").GetBytes(postData);
try
{
//基于apache服务器,IIS发布的则不需要
ServicePointManager.Expect100Continue = false;
CookieContainer cookieContainer = new CookieContainer();
//创建对url的请求
httpWebRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/QVOD, application/QVOD, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-xpsdocument, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpWebRequest.Headers["Accept-Language"] = "zh-cn";
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//协议方式
httpWebRequest.Method = "POST";
//post开始
//请求内容长度
httpWebRequest.ContentLength = byteArray.Length;
Stream dataStream = httpWebRequest.GetRequestStream();
// 请求数据放入请求流
dataStream.Write(byteArray, 0, byteArray.Length); //返回html
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("gb2312"));
//读取响应流
responseFromServer = reader.ReadToEnd();
if (responseFromServer.Contains("success"))
{
MessageBox.Show("登陆成功!!");
gCookieCollention = httpWebResponse.Cookies; //保存登陆成功后的cookie
}
else
MessageBox.Show("用户名或密码错误。");
MessageBox.Show(responseFromServer); //读响应流html
reader.Close();
dataStream.Close();
httpWebResponse.Close();
}
} catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} private void btn_Reply_Click(object sender, EventArgs e)
{
VisitUrl(txt_url.Text); //这一步还是未登录状态
}
private void VisitUrl(string url) //转到帖子所在界面
{
CookieContainer cc = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.CookieContainer = cc;
request.KeepAlive = true;
request.CookieContainer.Add(gCookieCollention);
HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(reponse.GetResponseStream(), Encoding.GetEncoding("gb2312"));
string s = sr.ReadToEnd();
txt_Msg.Text = s;
}
}
}
c#.netwindows
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货