Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用HttpWebRequest或MetaWeblog API在DotClear博客中添加类别_C#_Httpwebrequest - Fatal编程技术网

C# 如何使用HttpWebRequest或MetaWeblog API在DotClear博客中添加类别

C# 如何使用HttpWebRequest或MetaWeblog API在DotClear博客中添加类别,c#,httpwebrequest,C#,Httpwebrequest,我正在尝试创建/修改dotclear博客 对于大多数选项,我使用xmlrpcapi(DotClear.MetaWeblog)。但我没有找到任何处理类别的方法 因此,我开始研究Http数据包,并尝试“与浏览器相同” 下面是我用来“Http POST”的方法 因此,首先要做的是作为管理员进行身份验证。代码如下: protected bool HttpAuthenticate() { Uri u = new Uri(this.Url); Uri url = new Uri(strin

我正在尝试创建/修改dotclear博客

对于大多数选项,我使用xmlrpcapi(DotClear.MetaWeblog)。但我没有找到任何处理类别的方法

因此,我开始研究Http数据包,并尝试“与浏览器相同”

  • 下面是我用来“Http POST”的方法

  • 因此,首先要做的是作为管理员进行身份验证。代码如下:

    protected bool HttpAuthenticate()
    {
        Uri u = new Uri(this.Url);
        Uri url = new Uri(string.Format("{0}/admin/auth.php", u.GetLeftPart(UriPartial.Authority)));
        string data = string.Format("user_id={0}&user_pwd={1}&user_remember=1", Username, Password);
        var ret = HttpPost(url,data,false);
        return (ret == HttpStatusCode.OK || ret==HttpStatusCode.Found);
    }
    
    string postData = string.Format("cat_title={0}&new_cat_parent={1}&xd_check={2}", category_, 0, xdCheck);
    HttpPost(url, postData, true);
    
  • 现在我已经通过了身份验证,我需要获得一个
    xd_chek
    信息(我可以在页面上找到这个信息,所以基本上它是一个
    /admin/category.php
    +Regex(
    dotclear[.]nonce='(.*)

  • 因此,我通过了身份验证,并进行了
    xd\u检查
    info。最后要做的事情似乎是发布下一个类别。但当然它根本不起作用。。。代码如下:

    protected bool HttpAuthenticate()
    {
        Uri u = new Uri(this.Url);
        Uri url = new Uri(string.Format("{0}/admin/auth.php", u.GetLeftPart(UriPartial.Authority)));
        string data = string.Format("user_id={0}&user_pwd={1}&user_remember=1", Username, Password);
        var ret = HttpPost(url,data,false);
        return (ret == HttpStatusCode.OK || ret==HttpStatusCode.Found);
    }
    
    string postData = string.Format("cat_title={0}&new_cat_parent={1}&xd_check={2}", category_, 0, xdCheck);
    HttpPost(url, postData, true);
    

  • 我哪里出错了?

    我终于找到了解决办法

    看起来,cookies在命名和使用页面之间丢失了

    因此,我决定不使用框架的de-CookiesContainer,而只是将cookie保存在字符串中,并在每次请求时传递它们

    而且效果很好

    代码示例:

    private string _cookieAsString = string.Empty;
    
        protected string CookieAsString
        {
            get { return _cookieAsString; }
            set
            {
                if (value != null)
                {
                    if (!_cookieAsString.Contains(value))
                    {
                        if (_cookieAsString.Length == 0)
                            _cookieAsString = value;
                        else
                            _cookieAsString += string.Format(";{0}", value);
                    }
                }
            }
        }
    
    在HttpWebRequest中,我将其设置为:

    ...
    Request.Headers.Add(HttpRequestHeader.Cookie, CookieAsString);
    ...
    
    并将cookies保存在httpWebRequest中,如下所示:

    ...
    CookieAsString = Response.Headers[HttpResponseHeader.SetCookie];
    ...