Я пытаюсь загрузить содержимое текстового файла с веб-сайта ASP.NET. После многократной помощи Stack-overflow и других веб-сайтов мне удалось войти на сайт и получить аутентификацию. Тем не менее, мой второй запрос на отправку - это загрузить текстовый файл, который просто возвращает мне заголовки текстового файла, который разделен на канал (|). Я очень близок к выполнению этой задачи, и только экспертные взгляды могут получить меня через эту легкую кажущуюся трудную задачу в течение дня. Ниже приведен весь код. Если кто-то может сообщить мне, где я могу добавить что-то, что заставит трюк работать.Не удалось получить содержимое сообщения HTTP с сайта ASP.NET
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;
}
}
}