2009-08-26 100 views
7

我試圖以編程方式提交特定的表單,但我總是將首頁返回。 我必須在這裏做錯事或缺少些事。 即時通訊發送會話cookie和一些POST數據像viewState(我從初始請求解析),和SessionID(這是我的形式更改從其他年份的數據更改數據)。但在第二次請求我讓永諾的數據會話899,而不是一個我請求:875以編程方式提交表單

下面是使用的代碼:,任何幫助是非常apreciated

retrieveEdmIndexForSession(875);

protected string retrieveEdmIndexForSession(int sessionId) { 

    CookieContainer cookies; 
    HttpWebRequest oRequest; 
    HttpWebResponse oResponse; 
    Stream sw; 
    StreamReader sr; 
    string pageData; 
string PathRemote = @"http://edmi.parliament.uk/EDMi/EDMList.aspx"; 


    /* 
    * download the index page so we can get Cookies and ViewState from it. 
    */ 
    oRequest = (HttpWebRequest)WebRequest.Create(PathRemote); 
    oRequest.Method = "GET"; 
    oRequest.AllowAutoRedirect = true; 
    oRequest.CookieContainer = new CookieContainer(); 
    oRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
    oRequest.Referer = "http://edmi.parliament.uk/EDMi/EDMList.aspx"; 


    oResponse =(HttpWebResponse) oRequest.GetResponse(); 
    sr = new StreamReader(oResponse.GetResponseStream()); 

    pageData = sr.ReadToEnd(); 

    /* 
    * extract view state from pageData. 
    */ 
    string viewState = this.ExtractViewState(pageData); 


    /* 
    * lets submit the form with the parameters we want 
    */ 
    oRequest = (HttpWebRequest)WebRequest.Create(PathRemote); 
    oRequest.Method = "POST"; 
    oRequest.AllowAutoRedirect = true; 
    oRequest.ContentType = "application/x-www-form-urlencoded"; 
    oRequest.CookieContainer = new CookieContainer(); 
    oRequest.CookieContainer.Add(oResponse.Cookies); 
    oRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
    oRequest.Referer = "http://edmi.parliament.uk/EDMi/EDMList.aspx"; 

    string postdata = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=" + viewState + "&_MenuCtrl%3AddlSession=" + sessionId + "&_MenuCtrl%3A_GoTo.x=57&_MenuCtrl%3A_GoTo.y=14&ddlStatus=1&ddlSortedBy=1"; 
    byte[] buffer = Encoding.UTF8.GetBytes(postdata); 
    oRequest.ContentLength = buffer.Length; 

    /* 
    * Send post data into request stream first 
    */ 
    sw = oRequest.GetRequestStream(); 
    sw.Write(buffer, 0, buffer.Length); 
    sw.Flush(); 
    sw.Close(); 

    /* 
    * Connect, send and get response 
    */ 
    oResponse = (HttpWebResponse)oRequest.GetResponse(); 
    sr = new StreamReader(oResponse.GetResponseStream()); 

    //OnLogUpdated(1, "\r\nStatus Code: " + oResponse.StatusCode); 
    //OnLogUpdated(1, "\r\nServer: " + oResponse.Server); 

    pageData = sr.ReadToEnd(); 
    string result = getSessionId(pageData); 
    //OnLogUpdated(1, "\r\nRestuls: [" + result + "]"); 
    //OnLogUpdated(1, "\r\nPage: [" + pageData + "]"); 


    return pageData; 


} 



private string ExtractViewState(string str) 
{ 
    string viewState = ""; 
    string pattern = "(?<=__VIEWSTATE\" value=\")(?<val>.*?)(?=\")"; 

    Match match = Regex.Match(str, pattern); 

    if (match.Success) 
    { 
     viewState = match.Groups["val"].Value; 
     viewState = HttpUtility.UrlEncodeUnicode(viewState); 

    } 

    return viewState; 
} 

protected string getSessionId(string str) 
{ 
    string sessionId = string.Empty; 

    str = str.Trim(); 

    string pattern = @"&SESSION=([^']+)'"; 

    Match match = Regex.Match(str, pattern, RegexOptions.IgnoreCase); 
    if (match.Success) 
    { 
     sessionId = match.Groups[1].ToString(); ; 

    } 

    return sessionId; 
} 

這是由.NET腳本發送的RAW請求。

POST /EDMi/EDMList.aspx HTTP/1.1 內容類型: 應用/ X WWW的窗體-urlencoded 接受: text/html的,應用/ XHTML + xml的,應用/ XML; Q = 0.9,/; q = 0.8的Referer : http://edmi.parliament.uk/EDMi/EDMList.aspx 的User-Agent:.NET框架客戶 主機:edmi.parliament.uk餅乾: ASP.NET_SessionId = k55fqarvx2oszp2wxhtrol45 內容長度:2431期望: 100 - 繼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1PjtpPDc%2bOz47bDx0PHA8cDxsPFRleHQ7PjtsPDIxMDk7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPEVETXMgYW5kIEFtZW5kbWVudHM7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPDE7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPDUwOz4%2bOz47Oz47Pj47Pj47Pj47Pj47bDxfTWVudUN0cmw6X0dvVG87Pj5NHcFbPBNzNuwxs7sYLdUE2omkjw%3D%3D & _MenuCtrl%3AddlSession = 875 & _MenuCtrl%3A_GoTo.x = 57 & _MenuCtrl%3A_GoTo。Y = 14個& ddlStatus = 1 & ddlSortedBy = 1

這是由IE發送的原始請求:

POST /EDMi/EDMList.aspx HTTP/1.1 接受:圖像/ GIF,圖像/ JPEG, 圖像/ PJPEG,圖像/ PJPEG, 應用程序/ x-衝擊波閃光, 應用/ XAML + xml的, 應用/ vnd.ms-xpsdocument, 應用/ X-MS-XBAP, 應用程序/ x -ms-application, application/vnd.ms-exc EL, 應用程序/ vnd.ms - PowerPoint中, 應用程序/ msword,/的Referer: http://edmi.parliament.uk/EDMi/EDMList.aspx 接受語言:EN-GB的User-Agent:Mozilla的 /4.0(兼容; MSIE 8.0; Windows NT 5.1;三叉戟/ 4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; InfoPath.1; .NET CLR 3.0.04506.648; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)Content-Type: application/x-www-form-urlencoded Accept-Encoding:gzip,deflate Host: edmi.parliament.uk Content-Length: 2431 Connection:Keep-Alive Pragma: no-cache Cookie: WT_FPC = id = 83.217.99.254-2364242496.30021299:lv = 1249572414567:ss = 1249572414567; ASP.NET_SessionId = vwxgo4rlex1j5m55l0bivrqo

__EVENTTARGET = & __EVENTARGUMENT = & __VIEWSTATE = dDwxMDgyNzIxNDQ2O3Q8O2w8aTwzPj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sPFRleHQ7PjtsPEVETXMgYW5kIEFtZW5kbWVudHM7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPDE7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPDUwOz4%2BOz47Oz47Pj47Pj47Pj47Pj47bDxfTWVudUN0cmw6X0dvVG87Pj5NHcFbPBNzNuwxs7sYLdUE2omkjw%3D%3D & _MenuCtrl%3AddlSession = 885 & ddlStatus = 0 & ddlSortedBy = 1個& _MenuCtrl%3A_GoTo。X = 37 & _MenuCtrl%3A_GoTo.y = 12

的IE頭似乎有一個額外的cookie(WT_FPC = ID = 83.217.99.254-2364242496.30021299:LV = 1249572414567:SS = 1249572414567)巫appers跟蹤訪客通過WebTrends Cookie插件使用Cookie。這兩個POST請求均返回HTTP狀態碼302,並重定向到返回狀態200的GET請求。

任何想法?

+0

你有沒有嘗試過把提琴手和真正的請求區別開來,而不是編程的呢?在某個地方一定有區別。 – Pike65

+0

我一直在試圖用小提琴來解決這個問題。我將使用請求的RAW數據更新帖子。 –

回答

2

我已經破解了它。似乎.NET服務器不喜歡我同時更改狀態和會話。 它的工作原理,如果我改變從 「ddlStatus」:

串POSTDATA = 「__EVENTTARGET = & __EVENTARGUMENT = & __VIEWSTATE =」 + VIEWSTATE + 「& _MenuCtrl%3AddlSession =」 +的sessionId +「& _MenuCtrl %3A_GoTo.x = 57 & _MenuCtrl%3A_GoTo.y = 14 & ddlStatus = 1 & ddlSortedBy = 1「;

這樣:

串POSTDATA = 「__EVENTTARGET = & __EVENTARGUMENT = & __VIEWSTATE =」 + VIEWSTATE + 「& _MenuCtrl%3AddlSession =」 +的sessionId +「& _MenuCtrl%3A_GoTo。 x = 57 & _MenuCtrl%3A_GoTo.y = 14 & ddlStatus = 0 & ddlSortedBy = 1「;

我將不得不在第二個POST請求中更改ddlStatus,以此類推,對於表單中的每個更改。 .NET似乎受過很好的教育,一次只能拿一個甜點。

感謝所有幫助:)

1

也許這與您的問題沒有關係,但有時當您抓取一個站點時,遠程服務器會識別出正在瀏覽的人是腳本並阻止某些操作。我通常通過將UserAgent屬性設置爲請求來解決此問題。這是一個糟糕的安全措施,但經常用於我的經驗。

Here you can find some sample user agent strings

+0

我嘗試過不同的UserAgent,但沒有成功。這似乎不是問題。 –

1

在那個請求中,唯一令我感到奇怪的是Expect標題。 Apparently it's caused problems for people in the past。要停止,您可以使用以下行:

如果是這樣,它可能值得設置該屬性回不管它是之前的問題,因爲它是一個全局變量,可能會影響你的代碼的其他部分。

+0

派克65:我只是在看這個。我設置了oRequest.ServicePoint.Expect100Continue = false;這從標題中刪除行,但仍然是我的問題仍然:( –

3

您在每次創建一個新的CookieContainer。嘗試使用索引頁面請求的CookieContainer作爲表單提交的容器,它應該包含額外的cookie。或者,表單之外的其他內容可能會傳遞額外的Cookie。使用IEinspector的HttpAnalyzer或Firefox的Firebug加載頁面,以確保其他表單元素(如圖像)未通過Cookie。

相關問題