2014-02-15 38 views
4

我想將數據發佈到網站上。我處理餅乾此代碼:將數據發佈到網站上使用cookies和瀏覽器狀態

CookieCollection cookies = new CookieCollection(); 
     request.CookieContainer = new CookieContainer(); 
       request.CookieContainer.Add(cookies);  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

我處理視圖狀態的值與此代碼:

var doc = new HtmlAgilityPack.HtmlDocument(); 
       doc.Load(resp.GetResponseStream()); 
       foreach (HtmlNode input in doc.DocumentNode.SelectNodes("//input")) 
       {     
       if (input.Attributes["value"] != null) 
        { 
        val = input.Attributes["value"].Value; 
        if (val.IndexOf("1")!=-1) 
         { 
         viewState = val; 
         } 
        } 
       } 

,最後我與這個代碼的發佈數據:

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
         cookies = response.Cookies; 
         response.Close(); 
         string getUrl = "url"; 
         string postData = String.Format(""+viewstate); 
         HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl); 
         getRequest.CookieContainer = new CookieContainer(); 
         getRequest.CookieContainer.Add(cookies); 

我的主要問題是viewState,因爲如果我不發佈viewstate,它會返回同一頁面,或者如果我在發佈數據之前解析viewstate值併發布數據,它會將我的會話超時。例如,我可以登錄Facebook,但無法將數據發佈到使用viewState的網站。我無法弄清楚這種情況。我認爲我必須在post中解析viewstate,但我知道webrequest不能被重用。你可以幫我嗎?

+0

你可以分享鏈接,你想做什麼? – Yozer

+0

apps.db.ripe.net/syncupdates/simple-rpsl.html – user3297904

回答

3

我使用CsQuery庫。這是有用的和易於使用。 https://www.nuget.org/packages/CsQuery

使用一些類這樣的:

public class WebClientEx : WebClient 
{ 
    public CookieContainer _cookies = new CookieContainer(); 

    public string Get(string URL) 
    { 
     return this.DownloadString(URL); 
    } 

    public WebClientEx() 
    { 
     this.Encoding = Encoding.UTF8; 
     System.Net.ServicePointManager.Expect100Continue = false; 
    } 

    protected override WebRequest GetWebRequest(Uri address) 
    { 

     var request = (HttpWebRequest)base.GetWebRequest(address); 
     request.Host = "apps.db.ripe.net"; 
     request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0"; 
     request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
     request.KeepAlive = true; 
     request.Proxy = new WebProxy(); 
     request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; 
     request.CookieContainer = _cookies; 
     request.Headers.Add("Accept-Language", "en-us;q=0.7,en;q=0.3"); 
     request.ContentType = "application/x-www-form-urlencoded"; 

     return request; 
    } 

    public string Post(string URL, NameValueCollection data) 
    { 
     return this.Encoding.GetString(this.UploadValues(URL, data)); 
    } 
} 

設置後的參數,只要你想。 然後只是做:

 WebClientEx client = new WebClientEx(); 
     string Page = client.Get("https://apps.db.ripe.net/syncupdates/simple-rpsl.html"); //get some cookies and viewstate 

     CQ dom = CQ.Create(Page); 
     string viewstate = dom["input#javax\\.faces\\.ViewState"].Val(); //get viewstate 

     NameValueCollection data = new NameValueCollection(); 
     data.Add("rpslBox:postRpsl", "rpslBox:postRpsl"); 
     data.Add("rpslBox:postRpsl:sourceRadioSelect", "RIPE_NCC"); 
     data.Add("rpslBox:postRpsl:rpslObject", "your some string"); //your string 
     data.Add("rpslBox:postRpsl:update", "Update"); 
     data.Add("javax.faces.ViewState", viewstate); 

     Page = client.Post("https://apps.db.ripe.net/syncupdates/simple-rpsl.html", data); 
+0

我遇到錯誤:類型或命名空間的NameValueCollection could'nt找到 爲什麼? – user3297904

+0

添加正確的使用:System.Collections.Specialized – Yozer

+0

感謝它的工作很好。我還有一個問題。我不明白它是如何工作的。它在一個連接中發出多個請求? – user3297904

相關問題