首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用DotClear HttpWebRequest或MetaWeblog API在MetaWeblog博客中添加类别

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

Stack Overflow用户
提问于 2009-11-10 16:52:19
回答 1查看 272关注 0票数 0

我正在尝试创建/修改点阵博客。

对于大多数选项,我使用XmlRpc API (DotClear.MetaWeblog)。但我没找到处理分类的方法。

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

  1. 这里是我用来"Http POST“的方法

受保护的HttpStatusCode HttpPost(Uri url_,string data_,bool allowAutoRedirect_) { HttpWebRequest请求;HttpWebResponse Response = null;Stream ResponseStream =空;Request = "Mozilla/5.0 ( Windows;U;Windows 5.1;fr;rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR3.5.30729)“;Request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";Request.AllowAutoRedirect = allowAutoRedirect_;//向请求中添加网络凭据。Request.Credentials =新NetworkCredential(用户名、密码);字符串authInfo =用户名+ ":“+密码;authInfo =”基本“+ authInfo;Request.Method = "POST";Request.CookieContainer = Cookies;if (ConnectionCookie= null) Request.CookieContainer.Add(url_,ConnectionCookie);if (dcAdminCookie != null) Request.CookieContainer.Add(url_,dcAdminCookie);Request.PreAuthenticate = true;ASCIIEncoding编码=新ASCIIEncoding();string postData = data_;byte[] data = encoding.GetBytes(postData);//Encoding.UTF8.GetBytes(data_);//encoding.GetBytes(postData);Request.ContentLength = data.Length;Request.ContentType =“application/x form-urlencoded”;Stream newStream = Request.GetRequestStream();//发送数据。newStream.Write(data,0,data.Length);newStream.Close();尝试从服务器获得响应。响应= (HttpWebResponse)Request.GetResponse();if (!allowAutoRedirect_) { foreach (Cookie c in Response.Cookies) { if (c.Name == "dcxd") ConnectionCookie = c;if (c.Name == "dc_admin") dcAdminCookie = c;} Cookies.Add(Response.Cookies);} //获取响应流。ResponseStream = Response.GetResponseStream();//使用所需的编码格式将流输送到更高级别的流读取器。StreamReader readStream =新StreamReader(ResponseStream,Encoding.UTF8);字符串结果= readStream.ReadToEnd();if (Request.RequestUri == Response.ResponseUri) { _log.InfoFormat("{0} ==> {1}({2})",Request.RequestUri,Response.StatusCode,Response.StatusDescription);}{ _log.WarnFormat("RequestUri:{0}\r\nResponseUri:{1}\r\nstatus代码:{2}状态下降:{3}“,Request.RequestUri,Response.ResponseUri,Response.StatusCode,Response.StatusDescription);} catch (WebException wex) { Response = wex.Response as HttpWebResponse;if (响应!=空){ _log.ErrorFormat("{0} ==> {1}({2})",Request.RequestUri,Response.StatusCode,Response.StatusDescription);} Request.Abort();}最后{ if (响应!= null) { //释放响应的资源。Response.Close();}}if(响应!=null)返回Response.StatusCode;返回HttpStatusCode.Ambiguous;}

  • ,所以要做的第一件事是作为admin进行身份验证。以下是代码:

受保护的bool HttpAuthenticate() { Uri u=新Uri(this.Url);Uri url =新Uri(string.Format("{0}/admin/auth.php",u.GetLeftPart(UriPartial.Authority);string data =HttpAuthenticate用户名、密码);var ret = HttpPost(url,data,false);返回(ret == HttpStatusCode.OK复ret==HttpStatusCode.Found);}

  • ,现在我已经通过身份验证了,我需要获得一个xd_chek信息(我可以在页面上找到它,所以基本上它是一个get on /admin/category.php +

  • ,我是经过身份验证的,并且有xd_check信息。最后一件事似乎是发布下一个类别。但当然它根本不起作用..。以下是代码:

字符串postData =postData category_,0,xdCheck);HttpPost(url,postData,true);

我哪里出问题了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-11-26 14:27:31

最后我找到了解决办法。

似乎曲奇饼在诱惑和使用页面之间丢失了。

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

而且效果很好!

代码示例:

代码语言:javascript
复制
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中,我是这样设置的:

代码语言:javascript
复制
...
Request.Headers.Add(HttpRequestHeader.Cookie, CookieAsString);
...

将饼干保存在httpWebRequest中,如下所示:

代码语言:javascript
复制
...
CookieAsString = Response.Headers[HttpResponseHeader.SetCookie];
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1709449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档