Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8323747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:49:05+00:00 2026-06-08T23:49:05+00:00

I have to scrape a table from a secure site and I’m having trouble

  • 0

I have to scrape a table from a secure site and I’m having trouble logging in to the page and retrieving the authentication token and any other associated cookies. Am I doing something wrong here?

public NameValueCollection LoginToDatrose()
{
    var loginUriBuilder = new UriBuilder();
    loginUriBuilder.Host = DatroseHostName;
    loginUriBuilder.Path = BuildURIPath(DatroseBasePath, LOGIN_PAGE);
    loginUriBuilder.Scheme = "https";

    var boundary = Guid.NewGuid().ToString();
    var postData = new NameValueCollection();
    postData.Add("LoginName", DatroseUserName);
    postData.Add("Password", DatrosePassword);

    var data = Encoding.ASCII.GetBytes(postData.ToQueryString(false));
    var request = WebRequest.Create(loginUriBuilder.Uri) as HttpWebRequest;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;
    using (var d = request.GetRequestStream())
    {
        d.Write(data, 0, data.Length);
    }

    var response = request.GetResponse() as HttpWebResponse;
    var responseCookies = new NameValueCollection();
    foreach (var nvp in response.Cookies.OfType<Cookie>())
    {
        responseCookies.Add(nvp.Name, nvp.Value);
    }

    //using (var responseData = response.GetResponseStream())
    //using (var responseReader = new StreamReader(responseData))
    //{
    //    var theResponse = responseReader.ReadToEnd();
    //    Debug.WriteLine(theResponse);
    //}

    return responseCookies;

}

I get no values in the return object. It does not fail. The value of theResponse (when not commented out) seems to be the HTML of the login page.

Any assistance would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:49:06+00:00Added an answer on June 8, 2026 at 11:49 pm

    OK, the problem here seems related to the 302 redirect that would occur after the credentials were passed. The HttpWebRequest would automatically follow the 302.

    Ultimately, I ended up doing things a little differently. First, I subclassed the WebClient class as follows:

    public class CookiesAwareWebClient : WebClient
    {
        private CookieContainer outboundCookies = new CookieContainer();
        private CookieCollection inboundCookies = new CookieCollection();
    
        public CookieContainer OutboundCookies
        {
            get
            {
                return outboundCookies;
            }
        }
        public CookieCollection InboundCookies
        {
            get
            { 
                return inboundCookies; 
            }
        }
    
        public bool IgnoreRedirects { get; set; }
    
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = outboundCookies;
                (request as HttpWebRequest).AllowAutoRedirect = !IgnoreRedirects;
            }
            return request;
        }
    
        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            if (response is HttpWebResponse)
            {
                inboundCookies = (response as HttpWebResponse).Cookies ?? inboundCookies;
            }
            return response;
        }
    }
    

    This allowed me to use a WebClient class that was cookies-aware as well as one that I could control the redirect. Then I rewrote my code for logging in as follows:

    public NameValueCollection LoginToDatrose()
    {
        var loginUriBuilder = new UriBuilder();
        loginUriBuilder.Host = DatroseHostName;
        loginUriBuilder.Path = BuildURIPath(DatroseBasePath, LOGIN_PAGE);
        loginUriBuilder.Scheme = "https";
    
        var postData = new NameValueCollection();
        postData.Add("LoginName", DatroseUserName);
        postData.Add("Password", DatrosePassword);
    
        var responseCookies = new NameValueCollection();
    
        using (var client = new CookiesAwareWebClient())
        {
            client.IgnoreRedirects = true;
            var clientResponse = client.UploadValues(loginUriBuilder.Uri, "POST", postData);
            foreach (var nvp in client.InboundCookies.OfType<Cookie>())
            {
                responseCookies.Add(nvp.Name, nvp.Value);
            }
        }
    
        return responseCookies;
    }
    

    …and everything worked swimmingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using watir-webdriver to scrape from a page with nested table based layout.
I have a malformed page to scrape, and have had a hard time getting
I have a list of URLs from which I want to scrape an attribute.
Lets say I have a database table called Scrape possibly setup like: UserID (int)
I'm an amateur programmer wanting to scrape data from a site that is similar
I need to scrape data from a site, but it requires my login first.
Basically, I want to scrape some options data daily from Yahoo! Finance. I have
I have a site that scrapes all the episodes from tv.com from certain series.
I need to scrape some website data from a table on a website and
I have scraped a table from a website using C# for my own website

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.