我需要通过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>
下面,使用招,我得到这样的标题:
但它不可能做手工。所以,我们需要自动执行此访问,模拟浏览器导航。
下面,它的我的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”
这里是我的小提琴手:
我可以看到,标题是不同的,我不知道这是否是问题。我不知道如何通过代码制作相同的标题。
目标网页是一个web窗体,它使用内部的ajax控件工具包,我知道这是一个很大的麻烦....所以,我需要找到一种方法来获得正确的HTML响应。
以下是我需要通过代码获取的目标页面。
一旦我是这个页面里面,我需要做出一些碎屑,得到一些内容,模拟点击。
但是,我的第一个挑战是绕过这一步,获得正确的响应。
任何人都知道我在做什么错了?任何提示前进?
好了,我现在提高了我的代码安达,我用来做后这个方法:
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;
}
而现在,我得到这个标题:
这是现在几乎相同...除了Cookie值,我不知道如何通过代码发送它。
低于形式发布的时候,用的Cookie强调...
当我用一个简单的形式,我的访问是小提琴手,我们可以看到下一个页面(重定向)是“/MatriculaCalouros/Default.aspx”,这是我试图通过代码获取的正确页面。
但是,对于一些问题是我的帖子的代码,重定向是不同的......我得到的回应是从“/ captacao/matricula”页面,这是一个证明我发送错误的帖子.. 。这页是一些错误...
所以,我想知道如果我没有发送的Cookie是问题或者如果有其他事情发送的代码。
我嗅到这个练习中出现的邪恶机器人吗? – ViRuSTriNiTy
'我可以看到标题不同,我不知道这是否是问题。“如果您可以突出显示您帖子中的不同之处,那么我们可能会更快地为您提供帮助。 – mjwills
@mjwills我更新了我的文章...我改进了我的代码,试图通过代码发送相同的POST请求,但我不知道如何发送“Cookie”(请参阅我的文章)。我不知道如果这是我的问题,,,根据一些朋友,我不会解决我的问题,导致目标页面是一个web表单与ajax控件工具包webcontrols ...是真的吗? – Olivertech