C# https login and download file

后端 未结 2 1558
栀梦
栀梦 2020-12-01 22:58

I have successful connected to the login page, however, i\'m not sure how to login and grab the file thats behind the login. Below is the code i\'m using to make the connec

相关标签:
2条回答
  • 2020-12-01 23:23

    Fist thing, you need to initiate the cookie container before the first request:

    CookieContainer cookies = new CookieContainer();
    

    then you need to pass it on each request(now you're just instantiating it again, thus losing all the cookies):

    request.CookieContainer = cookies;
    

    the response you're getting will fill the cookie container with the necessary cookies.

    Furthermore, as I see you are already doing, you need to perform a series of Request/Response on the website and track the cookies. Use a tool like Fiddler to see what exactly how you need to formulate your POST strings to correctly login to the website.

    0 讨论(0)
  • 2020-12-01 23:28

    Your code has the following problems that I can see:

    1. Doesn't properly handle the cookie container. CookieContainer should be initialized and then passed to your HttpWebRequest, not the other way around.
    2. Does not cleanup disposable objects. Failing to dispose an object can result in the object hanging around for quite a while before the garbage collector catches up with it.
    3. Does not account for the form action. Your form action will cause a submit to a different location.
    4. Unnecessarily performs the first operation as a POST. Use GET instead.
    5. Does not set the referer when performing the POST operation.

    Try the following code:

        Uri url = new Uri("http://app/templat");
        HttpWebRequest request = null;
    
        // Uncomment the line below only if you need to accept an invalid certificate, i.e. a self-signed cert for testing.
        // ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
        CookieContainer cookieJar = new CookieContainer();
    
        request = (HttpWebRequest)WebRequest.Create(url);
        request.CookieContainer = cookieJar;
        request.Method = "GET";
        HttpStatusCode responseStatus;
    
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            responseStatus = response.StatusCode;
            url = request.Address;
        }
    
        if (responseStatus == HttpStatusCode.OK)
        {
            UriBuilder urlBuilder = new UriBuilder(url);
            urlBuilder.Path = urlBuilder.Path.Remove(urlBuilder.Path.LastIndexOf('/')) + "/j_security_check";
    
            request = (HttpWebRequest)WebRequest.Create(urlBuilder.ToString());
            request.Referer = url.ToString();
            request.CookieContainer = cookieJar;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
    
            using (Stream requestStream = request.GetRequestStream())
            using (StreamWriter requestWriter = new StreamWriter(requestStream, Encoding.ASCII))
            {
                string postData = "j_username=user&j_password=user&submit=Send";
                requestWriter.Write(postData);
            }
    
            string responseContent = null;
    
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader responseReader = new StreamReader(responseStream))
            {
                responseContent = responseReader.ReadToEnd();
            }
    
            Console.WriteLine(responseContent);
        }
        else
        {
            Console.WriteLine("Client was unable to connect!");
        }       
    
    0 讨论(0)
提交回复
热议问题