2010-08-30 65 views
0

我想在wordpress博客中使用HttpWebRequest自动化一些东西。登录Wordpress使用HttpWebRequest

我试图让登录页面"http://mywebsite.net/wp-admin"

,然后尝试登录数据发布页面上

http://www.mywebsite.net/wp-login.php” 数据=“日志=管理员& PWD =输入mypassword & WP-提交=登录+在& redirect_to的HTTP =%3A%2F%2Fmywebsite.net%2Fwp管理员%2F & testcookie = 1"

我是不能够登录,我已经发现,笏是当使用浏览器的cookie发送到服务器,但使用HttpWebrequest的cookie不发送后,我配置cookiecontainer的httpwebrequest和其他方面工作正常,..

,也有“后”的要求主机上也变为“www.mywebsite.net”和 的“取”请求是“mywebsite.net”

请任何一个可以指导我的解决方案。

回答

3

你应该已经分享了一些代码。但是,我认为有一些cookie管理问题。 这是我在向网站提交数据时管理cookie的方式。您可以使用此管理方案代码将 登录到您的网站。

 public string postFormData(Uri formActionUrl, string postData) 
    { 

     //Make a HttpWebReguest first 

     //set cookiecontainer 

     gRequest.CookieContainer = new CookieContainer();// gCookiesContainer; 

     //Manage cookies 

     #region CookieManagement 

     if (this.gCookies != null && this.gCookies.Count > 0) 
     { 

      gRequest.CookieContainer.Add(gCookies); 
     } 


     try 
     { 

      //logic to postdata to the form 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 

     } 
     //post data logic ends 

     //Get Response for this request url 
     try 
     { 
      gResponse = (HttpWebResponse)gRequest.GetResponse(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex); 

     } 



     //check if the status code is ok 

      if (gResponse.StatusCode == HttpStatusCode.OK) 
      { 
       //get all the cookies from the current request and add them to the response object cookies 

       gResponse.Cookies = gRequest.CookieContainer.GetCookies(gRequest.RequestUri); 
       //check if response object has any cookies or not 


       if (gResponse.Cookies.Count > 0) 
       { 
        //check if this is the first request/response, if this is the response of first request gCookies 
        //will be null 
        if (this.gCookies == null) 
        { 
         gCookies = gResponse.Cookies; 
        } 
        else 
        { 
         foreach (Cookie oRespCookie in gResponse.Cookies) 
         { 
          bool bMatch = false; 
          foreach (Cookie oReqCookie in this.gCookies) 
          { 
           if (oReqCookie.Name == oRespCookie.Name) 
           { 
            oReqCookie.Value = oRespCookie.Value; 
            bMatch = true; 
            break; 
           } 
          } 
          if (!bMatch) 
           this.gCookies.Add(oRespCookie); 
         } 
        } 
       } 
     #endregion 



       StreamReader reader = new StreamReader(gResponse.GetResponseStream()); 
       string responseString = reader.ReadToEnd(); 
       reader.Close(); 
       return responseString; 
      } 
      else 
      { 
       return "Error in posting data"; 
      } 

    } 
+0

Thanks @CodeBuzz。我正在使用可可,但你的想法照亮了我的想法。谢谢! (让我先尝试一下,虽然:) – swdev 2011-09-04 14:21:57

+1

谢谢swdev,前进..! – Muse 2011-09-05 07:12:57