我正在尝试从ASP.NET网站下载文本文件内容。经过Stack-overflow和其他网站的大量帮助后,我可以设法登录网站并获得身份验证。但是,我的第二个帖子请求是下载一个文本文件,它只返回管道(|)分隔的文本文件的标题。我非常接近完成这项任务,只有专家的意见才能让我度过这一简单看似困难的任务超过一天。下面是整个代码。如果有人可以让我知道我可以在哪里添加一些可以使这个技巧发挥作用的东西。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Collections.Specialized;
using System.Web;
namespace Login_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SSLValidator.OverrideValidation();
CookieContainer cookies = new CookieContainer();
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("https://www.abcd.org/");
request1.CookieContainer = cookies;
request1.Method = "GET";
request1.KeepAlive = false;
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
System.IO.Stream responseStream = response1.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
string srcString = reader.ReadToEnd();
// get the page ViewState
string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
int j = srcString.IndexOf("\"", i);
string viewState = srcString.Substring(i, j - i);
// get page EventTarget
string eventTargetFlag = "id=\"__EVENTTARGET\" value=\"";
i = srcString.IndexOf(eventTargetFlag) + eventTargetFlag.Length;
j = srcString.IndexOf("\"", i);
string eventTarget = srcString.Substring(i, j - i);
// get page EventArgument
string eventArgumentFlag = "id=\"__EVENTARGUMENT\" value=\"";
i = srcString.IndexOf(eventArgumentFlag) + eventArgumentFlag.Length;
j = srcString.IndexOf("\"", i);
string eventArgument = srcString.Substring(i, j - i);
// get page manScript_HiddenFieldFlag
string manScript_HiddenFieldFlag = "id=\"manScript_HiddenField\" value=\"";
i = srcString.IndexOf(manScript_HiddenFieldFlag) + manScript_HiddenFieldFlag.Length;
j = srcString.IndexOf("\"", i);
string Script_HiddenField = srcString.Substring(i, j - i);
string submitButton = "";
string lang = "";
string usr1 = "";
string pwd1 = "";
// UserName and Password
string userName = "username";
string password = "password";
// Convert the text into the url encoding string
viewState = System.Web.HttpUtility.UrlEncode(viewState);
eventTarget = System.Web.HttpUtility.UrlEncode("");
eventArgument = System.Web.HttpUtility.UrlEncode("");
lang = System.Web.HttpUtility.UrlEncode("en-US");
submitButton = System.Web.HttpUtility.UrlEncode(submitButton);
usr1 = System.Web.HttpUtility.UrlEncode(usr1);
pwd1 = System.Web.HttpUtility.UrlEncode(pwd1);
// Concat the string data which will be submit
string formatString =
"Script_HiddenField={0}&__EVENTTARGET={1}&__EVENTARGUMENT={2}&__VIEWSTATE={3}&lng={4}&p$lt$TopRightZone$Login1$UserName={5}&p$lt$TopRightZone$Login1$Password={6}&p$lt$TopRightZone$Login1$btnLogon={7}&p$lt$MainZone$pageplaceholder$p$lt$MainZone$Login1$UserName={8}&p$lt$MainZone$pageplaceholder$p$lt$MainZone$Login1$Password={9}";
string postString =
string.Format(formatString, Script_HiddenField, eventTarget, eventArgument, viewState, lang, userName, password, submitButton, usr1, pwd1);
//Send the request to login to the ASP.NET website
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.abcd.org/");
request.CookieContainer = cookies;
var data = Encoding.ASCII.GetBytes(postString.ToString());
request.Method = "POST";
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.ProtocolVersion = HttpVersion.Version10;
request.AllowAutoRedirect = true;
request.Headers.Add("Accept-Encoding", "gzip, deflate");
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
i = responseString.IndexOf(viewStateFlag) + viewStateFlag.Length;
j = responseString.IndexOf("\"", i);
viewState = responseString.Substring(i, j - i);
// Concat the string data which will be submit
formatString =
"Script_HiddenField={0}&__EVENTTARGET={1}&__EVENTARGUMENT={2}&__VIEWSTATE={3}&lng={4}&p$lt$TopRightZone$ux$Download={5}";
postString =
string.Format(formatString, "", "", "", System.Web.HttpUtility.UrlEncode(viewState), System.Web.HttpUtility.UrlEncode("en-US"), System.Web.HttpUtility.UrlEncode("1"));
// Send request2 to download the text file
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create("https://www.abcd.org/download?t=1&c=123456");
request.CookieContainer = cookies;
data = Encoding.ASCII.GetBytes(postString.ToString());
request2.Method = "POST";
request2.KeepAlive = false;
request2.ContentType = "application/x-www-form-urlencoded";
request2.ContentLength = data.Length;
request2.ProtocolVersion = HttpVersion.Version10;
request2.AllowAutoRedirect = true;
request2.Headers.Add("Accept-Encoding", "gzip, deflate");
using (var stream = request2.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response2 = (HttpWebResponse)request2.GetResponse();
var responseString2 = new StreamReader(response2.GetResponseStream()).ReadToEnd();
}
}
public static class SSLValidator
{
private static bool OnValidateCertificate(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
public static void OverrideValidation()
{
ServicePointManager.ServerCertificateValidationCallback =
OnValidateCertificate;
ServicePointManager.Expect100Continue = true;
}
}
}
答案 0 :(得分:0)
这已经解决了。我的代码是正确的,这是一个网站故障。