简体   繁体   中英

Unable to get HTTP post content back from ASP.NET website

I am trying to download a text file content from an ASP.NET website. After lot of help from Stack-overflow and other websites, I could manage to login into the website and get the authentication. However, my second post request is to download a text file which just returns me the headers of the text file which is pipe (|) separated. I am very close to accomplish this task and only expert views can get me through this easy seeming difficult task for over a day now. Below is the entire code written. If someone can let me know where I can add something which will make the trick work.


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;
        }
    }
}

This has been resolved. My code is proper, it was a website glitch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM