2017-08-02 79 views
0

我需要通過C#代碼訪問webform。該網頁收到一個組合參數(74741432599; 14/01/1970; 1; 3997),該參數將重定向到另一個網頁,我將在其中搜索某些標籤。POST使用代碼C#沒有得到與瀏覽提交相同的響應

如果我使用簡單的html表單訪問網頁,如下面的代碼,一切正常。

<html> 
    <body> 
     <form id="formIntegracaoMatriculaCalouros" name="formIntegracaoMatriculaCalouros" action="http://dsrvwww4/MatriculaCalouros/Seguro/Login.aspx" method="POST"> 
      <input type='hidden' id='CPFeDataNascimento' name='CPFeDataNascimento' value='74741432599;14/01/1970;1;3997' /> 
      <input type="submit" name="enviar" /> 
     </form> 
    </body> 
</html> 

下面,使用招,我得到這樣的標題:

enter image description here

但它不可能做手工。所以,我們需要自動執行此訪問,模擬瀏覽器導航。

下面,它的我的C#代碼來模擬訪問:

public static string SendPost(string url, HttpParameters parameters, CookieContainer cookie) 
{ 
    string postdata = parameters != null ? parameters.ToString() : string.Empty; 
    byte[] postBytes = Encoding.ASCII.GetBytes(postdata); 

    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); 
    myRequest.CookieContainer = cookie; 
    myRequest.Method = "POST"; 
    myRequest.ContentType = "application/x-www-form-urlencoded"; 
    myRequest.ContentLength = postBytes.Length; 

    Stream sw = myRequest.GetRequestStream(); 
    sw.Write(postBytes, 0, postBytes.Length); 
    sw.Close(); 
    WebResponse response = myRequest.GetResponse(); 
    StreamReader sr = new StreamReader(response.GetResponseStream()); 

    string responseString = sr.ReadToEnd(); 

    sr.Close(); 

    return responseString; 
} 

它做一個POST請求http://dsrvwww4/MatriculaCalouros/Seguro/Login.aspx。 我的參數爲 「74741432599; 14/01/1970; 1; 3997」

這裏是我的小提琴手:

enter image description here

我可以看到,標題是不同的,我不知道這是否是問題。我不知道如何通過代碼製作相同的標題。

目標網頁是一個web窗體,它使用內部的ajax控件工具包,我知道這是一個很大的麻煩....所以,我需要找到一種方法來獲得正確的HTML響應。

以下是我需要通過代碼獲取的目標頁面。

enter image description here

一旦我是這個頁面裏面,我需要做出一些碎屑,得到一些內容,模擬點擊。

但是,我的第一個挑戰是繞過這一步,獲得正確的響應。

任何人都知道我在做什麼錯了?任何提示前進?

好了,我現在提高了我的代碼安達,我用來做後這個方法:

public static string SendPost(string url, HttpParameters parameters, CookieContainer cookie) 
{ 
    string postdata = parameters != null ? parameters.ToString() : string.Empty; 
    byte[] postBytes = Encoding.ASCII.GetBytes(postdata); 

    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); 
    myRequest.CookieContainer = cookie; 
    myRequest.Method = "POST"; 
    myRequest.ContentType = "application/x-www-form-urlencoded"; 
    myRequest.ContentLength = postBytes.Length; 
    myRequest.ProtocolVersion = HttpVersion.Version10; 
    myRequest.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*"; 
    myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; Touch; .NET4.0C; .NET4.0E; Tablet PC 2.0)"; 
    myRequest.Headers.Add(HttpRequestHeader.Pragma, "no-cache"); 
    myRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate"); 
    myRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "pt-BR,pt;q=0.5"); 
    myRequest.Headers.Add(HttpRequestHeader.Cookie, cookie.ToString()); 
    myRequest.KeepAlive = true; 

    Stream sw = myRequest.GetRequestStream(); 
    sw.Write(postBytes, 0, postBytes.Length); 
    sw.Close(); 
    WebResponse response = myRequest.GetResponse(); 
    StreamReader sr = new StreamReader(response.GetResponseStream()); 

    string responseString = sr.ReadToEnd(); 

    sr.Close(); 
    response.Close(); 

    return responseString; 
} 

而現在,我得到這個標題:

enter image description here

這是現在幾乎相同...除了Cookie值,我不知道如何通過代碼發送它。

低於形式發佈的時候,用的Cookie強調...

enter image description here

當我用一個簡單的形式,我的訪問是小提琴手,我們可以看到下一個頁面(重定向)是「/MatriculaCalouros/Default.aspx」,這是我試圖通過代碼獲取的正確頁面。

但是,對於一些問題是我的帖子的代碼,重定向是不同的......我得到的迴應是從「/ captacao/matricula」頁面,這是一個證明我發送錯誤的帖子.. 。這頁是一些錯誤...

所以,我想知道如果我沒有發送的Cookie是問題或者如果有其他事情發送的代碼。

+0

我嗅到這個練習中出現的邪惡機器人嗎? – ViRuSTriNiTy

+0

'我可以看到標題不同,我不知道這是否是問題。「如果您可以突出顯示您帖子中的不同之處,那麼我們可能會更快地爲您提供幫助。 – mjwills

+0

@mjwills我更新了我的文章...我改進了我的代碼,試圖通過代碼發送相同的POST請求,但我不知道如何發送「Cookie」(請參閱​​我的文章)。我不知道如果這是我的問題,,,根據一些朋友,我不會解決我的問題,導致目標頁面是一個web表單與ajax控件工具包webcontrols ...是真的嗎? – Olivertech

回答

0

我最好的來賓是您的靜態方法正在從服務器端代碼中調用,因爲您的HTML表單不使用服務器端ASP.NET用戶控件。嘗試將runat =「server」添加到控件,然後嘗試發佈並觀察標題出現方式的差異。

+1

你確定你明白這裏提到的是什麼嗎? –

+0

我想我一定不明白他在問什麼。他似乎不明白如何以編程方式製作標題。如果他想觀察如何生成標題,他應該使用ASP.NET Web窗體,除非這是ASP.NET MVC,在這種情況下,他應該只使用剃刀語法@form或其它任何東西。 –

+0

'他應該使用ASP.NET Web Forms'。所以我不能用控制檯應用程序做同樣的事情? –

1

要使代碼中的標題使用HttpWebRequest.Headers集合,請在其中添加標題。