Code
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Net;
using System.IO;
namespace baidupost
{
class SendtoBaiduTieba
{
public static void fatie(string username, string password, string title, string content, string word, out string loginState, out string sendState)
{
CookieContainer cookieContainer = new CookieContainer();
// 设置打开页面的参数
string URI = "https://passport.baidu.com/?login";
HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Accept="application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Embedded Web Browser from: http://bsalsa.com/; .NET CLR 2.0.50727; CIBA)";
request.KeepAlive = true;
// 接收返回的页面
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("GB2312"));
string srcString = reader.ReadToEnd();
///////////////////////////////////////////////////
// 2. 自动填充并提交 登陆 页面
///////////////////////////////////////////////////
// 用户名和密码
// 将文本转换成 URL 编码字符串
username = System.Web.HttpUtility.UrlEncode(username, Encoding.GetEncoding("GB2312"));
word = System.Web.HttpUtility.UrlEncode(word, Encoding.GetEncoding("GB2312"));
// 要提交的字符串数据。格式形如:
string formatString =
"tpl_ok=&next_target=&tpl=&skip_ok=&aid=&need_pay=&need_coin=&pay_method=&u=.%2F&return_method=get&more_param=&return_type=&psp_tt=0&username={0}&password={1}";
string postString =
string.Format(formatString, username, password);
// 将提交的字符串数据转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
// 设置提交的相关参数
request = WebRequest.Create(URI) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookieContainer;
request.ContentLength = postData.Length;
request.Accept = "application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Embedded Web Browser from: http://bsalsa.com/; .NET CLR 2.0.50727; CIBA)";
request.KeepAlive = true;
// 提交请求数据
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
// 接收返回的页面
response = request.GetResponse() as HttpWebResponse;
request.CookieContainer = cookieContainer;
responseStream = response.GetResponseStream();
reader = new StreamReader(responseStream, Encoding.GetEncoding("GB2312"));
srcString = reader.ReadToEnd();
if (srcString.IndexOf("location.href=url") > 1)
loginState = "登陆成功!\r\n";
else
loginState = "登录失败\r\n";
///////////////////////////////////////////////////
// 3. 打开 发帖 页面
///////////////////////////////////////////////////
// 设置打开页面的参数
URI = "http://tieba.baidu.com/f?kw=" + word;
request = WebRequest.Create(URI) as HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Embedded Web Browser from: http://bsalsa.com/; .NET CLR 2.0.50727; CIBA)";
request.KeepAlive = true;
request.Method = "GET";
request.CookieContainer = cookieContainer;
// 接收返回的页面
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new StreamReader(responseStream, Encoding.GetEncoding("GB2312"));
srcString = reader.ReadToEnd();
// 获取发帖所需参数 这里使用正则表达式获取
int formStart = srcString.IndexOf("return f_submit()", 0);
int formEnd = srcString.IndexOf("</form>", formStart);
string htmlForm = srcString.Substring(formStart, formEnd - formStart);
string ct = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=ct value=(?<key>.*?)>");
string tn = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=tn value=(?<key>.*?)>");
string lm = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=lm value=\"(?<key>.*?)\" >");
string z = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=z value=(?<key>.*?)>");
string sc = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=sc value=(?<key>.*?)>");
string cm = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=cm value=\"(?<key>.*?)\">");
string bs = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=bs value=\"(?<key>.*?)\">");
string str2 = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=str2 value=\"(?<key>.*?)\">");
string str3 = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=str3 value=\"(?<key>.*?)\">");
string str4 = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=str4 value=\"(?<key>.*?)\">");
string bu = FormatHtml.FormatHtmlCode(htmlForm, "<input type=hidden name=bu value=\"(?<key>.*?)\" id=bu>");
// 发帖标题和内容
// 将文本转换成 URL 编码字符串
title = System.Web.HttpUtility.UrlEncode(title, Encoding.GetEncoding("GB2312"));
content = System.Web.HttpUtility.UrlEncode(content, Encoding.GetEncoding("GB2312"));
// 要提交的字符串数据。
formatString =
"ct={0}&tn={1}&word={2}&lm={3}&z={4}&sc={5}&cm={6}&bs={7}&str2={8}&str3={9}&str4={10}&bu={11}&ti={12}&co={13}&str1=http%3A%2F%2F&ch1=on&Submit3=%B7%A2%B1%ED%CC%F9%D7%D3";
postString =
string.Format(formatString, ct, tn, word, lm, z, sc, cm, bs, str2, str3, str4, bu, title, content);
request = WebRequest.Create("http://tieba.baidu.com/f") as HttpWebRequest;
request.Method = "POST";
request.Referer = URI;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Embedded Web Browser from: http://bsalsa.com/; .NET CLR 2.0.50727; CIBA)";
request.KeepAlive = true;
request.CookieContainer = cookieContainer;
// 将提交的字符串数据转换成字节数组
postData = Encoding.ASCII.GetBytes(postString);
request.ContentLength = postData.Length;
// 提交请求数据
outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
// 接收返回的页面
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("GB2312"));
srcString = reader.ReadToEnd();
if (srcString.IndexOf("操作成功") > 1)
sendState = "发送成功!\r\n";
else
sendState = "发送失败\r\n";
}
}
}
调用
string loginState = "";
string sendState = "";
SendtoBaiduTieba.fatie("用户名", "密码","这本书在哪儿看啊","不知道在哪儿看,大家推荐个地放吧","恶魔法则",out loginState,out sendState);
实现了登陆百度贴吧,但是在发帖的时候返回结果为"请不要发广告贴!如您确认所发贴子并非广告贴,请到贴吧投诉吧进行反馈,非常感谢 点击返回 " 哪位大大帮我看看原因出在哪里