[C#] 纯文本查看 复制代码
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 DotNet.Utilities;
using System.Text.RegularExpressions;
namespace wordpressDEMO
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string cookie="";
string postdata = "log=admin&pwd=admin&rememberme=forever&wp-submit=%E7%99%BB%E5%BD%95&redirect_to=http%3A%2F%2Flocalhost%3A90%2Fwp-admin%2F&testcookie=1";
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL="http://localhost:90/wp-login.php",
Method="post",
ContentType = "application/x-www-form-urlencoded",
Postdata=postdata,
//Allowautoredirect=true,
};
HttpResult result = http.GetHtml(item);
Regex reg = new Regex(@"wordpress_\S*;");
MatchCollection mts = reg.Matches(result.Cookie);
foreach (Match itemformhash in mts)
{
cookie += itemformhash.Value;
}
textBox1.Text = cookie;
item = new HttpItem()
{
URL = "http://localhost:90/wp-admin/post-new.php",
Cookie = cookie,
};
result = http.GetHtml(item);
textBox1.Text = result.Html;
string _wpnonce = GetValue(result.Html, "name=\"_wpnonce\" value=\"", "\" />");
string uid = GetValue(result.Html, "name=\"user_ID\" value=\"", "\" />");
string postid = GetValue(result.Html, "name='post_ID' value='", "' />");
string meta_box_order_nonce = GetValue(result.Html, "name=\"meta-box-order-nonce\" value=\"", "\" />");
string closedpostboxesnonce = GetValue(result.Html, "name=\"closedpostboxesnonce\" value=\"", "\" />");
string samplepermalinknonce = GetValue(result.Html, "name=\"samplepermalinknonce\" value=\"", "\" />");
string _ajax_nonce_add_category = GetValue(result.Html, "name=\"_ajax_nonce-add-category\" value=\"", "\" />");
string _ajax_nonce_add_meta = GetValue(result.Html, "name=\"_ajax_nonce-add-meta\" value=\"", "\" />");
string encoding = GetValue(result.Html, "charset=", "\"");
postdata = string.Format("_wpnonce={0}&_wp_http_referer=%2Fwp-admin%2Fpost-new.php&user_ID={1}&action=editpost&originalaction=editpost&post_author={1}&post_type=post&original_post_status=auto-draft&referredby=http%3A%2F%2Flocalhost%3A90%2Fwp-login.php%3Fredirect_to%3Dhttp%253A%252F%252Flocalhost%253A90%252Fwp-admin%252Fpost-new.php%26reauth%3D1&_wp_original_http_referer=http%3A%2F%2Flocalhost%3A90%2Fwp-login.php%3Fredirect_to%3Dhttp%253A%252F%252Flocalhost%253A90%252Fwp-admin%252Fpost-new.php%26reauth%3D1&auto_draft=&post_ID={2}&meta-box-order-nonce={3}&closedpostboxesnonce={4}&post_title={5}&samplepermalinknonce={6}&content={7}&wp-preview=&hidden_post_status=draft&post_status=draft&hidden_post_password=&hidden_post_visibility=public&visibility=public&post_password=&aa=2014&mm=11&jj=06&hh=10&mn=37&ss=03&hidden_mm=11&cur_mm=11&hidden_jj=06&cur_jj=06&hidden_aa=2014&cur_aa=2014&hidden_hh=10&cur_hh=10&hidden_mn=37&cur_mn=37&original_publish=%E5%8F%91%E5%B8%83&publish=%E5%8F%91%E5%B8%83&post_format=0&post_category%5B%5D=0&post_category%5B%5D=1&newcategory=%E6%96%B0%E5%88%86%E7%B1%BB%E7%9B%AE%E5%BD%95%E5%90%8D&newcategory_parent=-1&_ajax_nonce-add-category={8}&tax_input%5Bpost_tag%5D=%E6%B5%8B%E8%AF%95&newtag%5Bpost_tag%5D=&excerpt=&trackback_url=&metakeyinput=&metavalue=&_ajax_nonce-add-meta={9}&advanced_view=1&comment_status=open&ping_status=open&post_name=&post_author_override=1", _wpnonce, uid, postid, meta_box_order_nonce, closedpostboxesnonce, "测试吧", samplepermalinknonce, "测试吧测试吧测试吧测试吧测试吧", _ajax_nonce_add_category, _ajax_nonce_add_meta);
item = new HttpItem()
{
URL = "http://localhost:90/wp-admin/post.php",
PostEncoding = System.Text.Encoding.GetEncoding(encoding.Trim()),
Method = "post",
ContentType = "application/x-www-form-urlencoded",
Postdata = postdata,
Cookie = cookie,
Referer = "http://localhost:90/wp-admin/post-new.php",
};
result = http.GetHtml(item);
textBox1.Text = result.Html;
}
/// <summary>
/// 获得字符串中开始和结束字符串中间得值
/// </summary>
/// <param name="str">字符串</param>
/// <param name="s">开始</param>
/// <param name="e">结束</param>
/// <returns></returns>
public static string GetValue(string str, string s, string e)
{
Regex rg = new Regex("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline);
return rg.Match(str).Value;
}
}
}