代码如下:// PostToBbs
string strUrl = @"http://bbs.xxx.com/post.php?action=newthread&fid=23&topicsubmit=yes";
Uri siteUri = new Uri("http://bbs.xxx.com/");
// DATA
//标题
string strSubject = this.tbxPostTitle.Text;
//内容
string strMessage = this.rtbPost.Text;
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "&subject=" + strSubject;
postData += "&message=" + strMessage;
byte[] data = encoding.GetBytes(postData); // Cookie
CookieContainer myCookieContainer = new CookieContainer();
//新建一个HttpWebRequest
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
myRequest.CookieContainer = myCookieContainer;
myRequest.Referer = @"http://bbs.xxx.com/post.php?action=newthread&fid=23";
// STREAM Send the data.
Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close(); // Get response 新建一个HttpWebResponse
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
//获取一个包含url的Cookie集合的CookieCollection
myResponse.Cookies = myCookieContainer.GetCookies(myRequest.RequestUri); //拿到了Cookie,再进行请求就能直接读取到登录后的内容了
myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
myRequest.CookieContainer = myCookieContainer;
myRequest.Referer = @"http://bbs.xxx.com/post.php?action=newthread&fid=23";
newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close(); myResponse = (HttpWebResponse)myRequest.GetResponse();
myResponse.Cookies = myCookieContainer.GetCookies(myRequest.RequestUri);
StreamReader reader = new StreamReader(myResponse.GetResponseStream(),Encoding.Default);
string content = reader.ReadToEnd();
reader.Close();
this.rtbPost.Text = content;
//this.rtbPost.Text = nn;
// 保存
DialogHelper.ShowInformationMessage("帖子提交成功!");但内容无法提交,为什么呢? 在线等
string strUrl = @"http://bbs.xxx.com/post.php?action=newthread&fid=23&topicsubmit=yes";
Uri siteUri = new Uri("http://bbs.xxx.com/");
// DATA
//标题
string strSubject = this.tbxPostTitle.Text;
//内容
string strMessage = this.rtbPost.Text;
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "&subject=" + strSubject;
postData += "&message=" + strMessage;
byte[] data = encoding.GetBytes(postData); // Cookie
CookieContainer myCookieContainer = new CookieContainer();
//新建一个HttpWebRequest
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
myRequest.CookieContainer = myCookieContainer;
myRequest.Referer = @"http://bbs.xxx.com/post.php?action=newthread&fid=23";
// STREAM Send the data.
Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close(); // Get response 新建一个HttpWebResponse
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
//获取一个包含url的Cookie集合的CookieCollection
myResponse.Cookies = myCookieContainer.GetCookies(myRequest.RequestUri); //拿到了Cookie,再进行请求就能直接读取到登录后的内容了
myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
myRequest.CookieContainer = myCookieContainer;
myRequest.Referer = @"http://bbs.xxx.com/post.php?action=newthread&fid=23";
newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close(); myResponse = (HttpWebResponse)myRequest.GetResponse();
myResponse.Cookies = myCookieContainer.GetCookies(myRequest.RequestUri);
StreamReader reader = new StreamReader(myResponse.GetResponseStream(),Encoding.Default);
string content = reader.ReadToEnd();
reader.Close();
this.rtbPost.Text = content;
//this.rtbPost.Text = nn;
// 保存
DialogHelper.ShowInformationMessage("帖子提交成功!");但内容无法提交,为什么呢? 在线等
NetworkCredential myCred = new NetworkCredential(
SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain);
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri("www.contoso.com"), "Basic", myCred);
myCache.Add(new Uri("app.contoso.com"), "Basic", myCred);
WebRequest wr = WebRequest.Create("www.contoso.com");
wr.Credentials = myCache;
谢谢,试一下。另外这种方法是需要每次都输入用户名密码了哦。有没有直接读COOKIE的方法呢?
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData); request = (HttpWebRequest)WebRequest.Create(sUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length); newStream.Close();
request.CookieContainer = cc;
response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
stream = response.GetResponseStream();
sHtml = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
如果再 ADD 就有两个COOKIE了。问题好象不是出在这里。
IE中登陆网站获得的COOKIE 和程序中获得的COOKIE值并不相同,这会不会是原因所在呢?