2017-08-02 37 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集合,请在其中添加标题。