我分析了登录网页时发送的标题,并看到了2个标题,一个302和一个200 我正在尝试做与浏览相同的事情,但在使用httpwebrequest的c#中,我认为这很容易,但我已经2天了 试图做。 下面是我试图登录时捕获的标题和我用来模拟请求的代码... 我想知道是否有人可以帮助我,指出我的错误在哪里以及该做什么......
使用代码302请求标题:
POST http://www4.uva.br/UniversusNet/Seguro/Login.aspx?ReturnUrl=%2fUniversusNet%2fNotasFaltasTotais.aspx HTTP/1.1
Host: www4.uva.br
Proxy-Connection: keep-alive
Content-Length: 455
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://www4.uva.br
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://www4.uva.br/UniversusNet/Seguro/Login.aspx?ReturnUrl=%2fUniversusNet%2fNotasFaltasTotais.aspx
Accept-Encoding: gzip, deflate
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: ASP.NET_SessionId=wlo0ks55tanotwriaraapd55
WITH FORM DATA:
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE4NzU1ODgxNTkPZBYCZg9kFgICAw9kFgICCQ9kFgICAQ9kFgICAQ9kFgICAQ8QZGQWAGQYAQU2Y3RsMDAkRm9ybXVsYXJpb0NvbnRlbnRQbGFjZUhvbGRlciRFc3RhZG9UZWxhTXVsdGlWaWV3Dw9kZmT14eU493cBliuPCSv6TJQbGDKjrA%3D%3D&__VIEWSTATEGENERATOR=7C9DFC57&ctl00%24FormularioContentPlaceHolder%24UsuarioTextBox=@@@@@@&ctl00%24FormularioContentPlaceHolder%24SenhaTextBox=@@@@@&ctl00%24FormularioContentPlaceHolder%24EntrarButton=Entrar
使用代码200请求标题:
POST http://www4.uva.br/UniversusNet/NotasFaltasTotais.aspx HTTP/1.1
Host: www4.uva.br
Proxy-Connection: keep-alive
Content-Length: 32020
Cache-Control: no-cache
Origin: http://www4.uva.br
X-MicrosoftAjax: Delta=true
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
Referer: http://www4.uva.br/UniversusNet/NotasFaltasTotais.aspx
Accept-Encoding: gzip, deflate
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: ASP.NET_SessionId=wlo0ks55tanotwriaraapd55; UniversusNet=53956EA1E1E2BB4F86B216AD708DC0160BCAF34768BD4BD9AE0BB6D7AA70A3E6B159AF2A07B653DAC7FD6DA08A7027D3AE6490ADD013ABBFF2318D05EF1DCEF70D05C3FDFDF454EB975971F02E0C61C07200479377919483AF56B2DB4497BBA2C70825F492F5FF7012E97AD357A4440440D1954D6738E6E710B4E705CE4B151A66B3924FD6F9DB4B2305EB5C314D165B29D1C6D0C3EB9C5A9DC398C34458988AFE02735011D809A25E841DD05C9F5647C580BFA911ECFC0AFBE099C1A9CF65C2FDEA986F61AC144929E82DEC00AF0C5C396B2658877350EDB89B38EF08DCC7E4DB42B51E0878EA15E397378381B75EB68AEEE43C066C7C0BB5C7221D1701CF94E70A1B38667FE00A2CAF9E20B1555C75E4C059EAE885DADEF52E332E3DCAFCCD687914FFB517F473DA575798C480DA32550FB3EE3A39F7ACBCCBB60D72E3079B07789FFE3AAD4795E5A6ED2A8663DCC178A2E14A9655F3DD2EF61A04465D01D10EA3B153B1116B5BCD16457C475D325928E3A6F08F8C6C917FAC21660C917543E5A92DD6D1FDC69C90FE02C1F88C5A3DF609153053A6D46DBF1261F77130962D7774DE3275505E05D23EB315A35E32F05FC49AA9629CA5822748103451E45CBDD3555B3C8D70C21C9F19B5BD2AA36FDFB58982E3C6DBE2D3677CD385011252A0D3050CF1B23A025CF541418EC485B7D81DDB182F8E8A06920DCE85B3FAD5D76B5A2F761C
我制定了代码来模拟这一行动:
HttpWebRequest x = (HttpWebRequest)HttpWebRequest.Create("http://www4.uva.br/UniversusNet/NotasFaltasTotais.aspx");
NameValueCollection collection = new NameValueCollection();
NameValueCollection postData = new NameValueCollection();
CookieContainer cookie = new CookieContainer();
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes("__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=/wEPDwULLTE4NzU1ODgxNTkPZBYCZg9kFgICAw9kFgICCQ9kFgICAQ9kFgICAQ9kFgICAQ8QZGQWAGQYAQU2Y3RsMDAkRm9ybXVsYXJpb0NvbnRlbnRQbGFjZUhvbGRlciRFc3RhZG9UZWxhTXVsdGlWaWV3Dw9kZmT14eU493cBliuPCSv6TJQbGDKjrA==&__VIEWSTATEGENERATOR=7C9DFC57&ctl00$FormularioContentPlaceHolder$UsuarioTextBox=@@@@&ctl00$FormularioContentPlaceHolder$SenhaTextBox=@@@@@&ctl00$FormularioContentPlaceHolder$EntrarButton=Entrar");
collection.Add("Origin","http://www4.uva.br");
collection.Add("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4");
x.Headers.Add(collection);
x.CookieContainer = cookie;
x.Method = "POST";
x.ContentType = "application/x-www-form-urlencoded";
x.ContentLength = postBytes.Length;
x.Host = "www4.uva.br";
x.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36";
x.AllowAutoRedirect = true;
x.Proxy = new WebProxy("http://10.10.10.52:8080", false);
x.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream dataStream = x.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(postBytes, 0, postBytes.Length);
dataStream.Close();
WebResponse y = x.GetResponse();
var reader = new StreamReader(y.GetResponseStream());
var html = reader.ReadToEnd();
richTextBox1.Text = html.ToString();
但是返回的是我做请求的同一页面,所以我错过了什么?
答案 0 :(得分:1)
你可能会在javascript中显示一些错误的同一页面!当您获得页面源时,HttpWebResponse无法显示它们。
以下内容可能有助于向服务器发送正确的数据:
数据字符串应包含用户名和密码
检查运行程序时是否输入了正确的用户名和密码。 用户名字段
ctl00$FormularioContentPlaceHolder$UsuarioTextBox%3D=USERNAME
密码字段
ctl00$FormularioContentPlaceHolder$SenhaTextBox%3D=PASSWORD
<强>编码
还记得您的浏览器会对输入数据进行编码,因此您应该将'/','=','+'等字符编码为'%2F','%3D','%2B'
string dataString="__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE4NzU1ODgxNTkPZBYCZg9kFgICAw9kFgICCQ9kFgICAQ9kFgICAQ9kFgICAQ8QZGQWAGQYAQU2Y3RsMDAkRm9ybXVsYXJpb0NvbnRlbnRQbGFjZUhvbGRlciRFc3RhZG9UZWxhTXVsdGlWaWV3Dw9kZmT14eU493cBliuPCSv6TJQbGDKjrA%3D%3D&__VIEWSTATEGENERATOR=7C9DFC57&ctl00%24FormularioContentPlaceHolder%24UsuarioTextBox=Username&ctl00%24FormularioContentPlaceHolder%24SenhaTextBox=Password&ctl00%24FormularioContentPlaceHolder%24EntrarButton=Entrar"
URL编码将字符转换为可以通过Internet传输的格式。 HTML URL Encoding Reference
而不是使用
byte[] postBytes = ascii.GetBytes(YourDataString);
使用
byte[] postBytes = Encoding.UTF8.GetBytes(YourDataString);
<强> _ViewState 强>
视图状态是ASP.NET页面框架用于在往返之间保留页面和控制值的方法。呈现页面的HTML标记时,页面的当前状态和回发期间必须保留的值将序列化为base64编码的字符串。然后将此信息放入视图状态隐藏字段或字段中。 ASP.NET View State Overview
最好通过发送GET请求获取'_ViewState'值并在响应中查找其值。