【发布时间】:2017-08-02 20:45:30
【问题描述】:
我需要通过 C# 代码访问网络表单。该网页接收一个组合参数(74741432599;14/01/1970;1;3997),该参数重定向到另一个网页,我将在其中抓取一些标签。
如果我使用简单的 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;
}
它向http://dsrvwww4/MatriculaCalouros/Seguro/Login.aspx 发出 POST 请求。 我的参数是“74741432599;14/01/1970;1;3997”
这是我的提琴手:
可以看到Header不一样,不知道是不是这个问题。我不知道如何通过代码制作相同的标题。
目标网页是一个webform,它内部使用了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;
}
现在,我得到了这个标题:
现在几乎一样...除了 Cookies 值,我不知道如何通过代码发送它。
以下是通过表单发布时的提琴手,突出显示 Cookie...
当我使用简单的表单进行访问时,我们可以看到下一页(重定向)是“/MatriculaCalouros/Default.aspx”,这是我试图通过代码获取的正确页面。
但是,对于某些问题是我的代码发布,重定向是不同的...我得到的响应来自“/captacao/matricula”页面,这证明我发送了错误的帖子...这个页面是某种错误...
所以,我想知道是不是我没有发送的 Cookie 有问题,还是有其他东西要通过代码发送。
【问题讨论】:
-
我有没有从这个练习中闻到一个邪恶的机器人?
-
I can see that the Header is different, and I dont know if this is the problem.如果您可以在帖子中突出显示不同之处,我们可能会更快地为您提供帮助。 -
@mjwills 我更新了我的帖子...我改进了我的代码,试图通过代码发送相同的 POST 请求,但我不知道如何发送“Cookie”(请参阅我的帖子)。我也不知道这是不是我的问题,,,据一些朋友说,我不会解决我的问题,因为目标页面是一个带有ajax控制工具包webcontrols的webform......是真的吗?
-
我会是 Cookies 是问题所在。您能检查您的 cookie 集合是否为非空吗?您是否先发送一个简单的 get 请求以实际从服务器获取 cookie,然后再将它们与您的帖子一起发回?
-
@PawełŁukasik 不。我没有。我意识到 cookie 是问题......但我不知道如何填充它。我该怎么做才能在发送我的帖子之前获取 cookie?
标签: c# html asp.net httpwebresponse