2010-08-30 63 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