0

So for example: https://www.website.com/index.php?action=get_products

This page/action has the following source code: <tr><td>Table</td></tr>

The index.php page has the following source code: <body>Hello</body>

When I use the following code I still get the index.php code and not the action page code:

Uri url = new Uri("https://www.website.com/index.php");
        HttpWebRequest request = null;

        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 = "?action=get_products";
                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!");
        }   

I found the code above in another stackoverflow thread. The problem is I have to login first with username user and password pass. How do I get this done using C#?

Community
  • 1
  • 1
  • Why have you tagged this `php`? Just because the html that you're trying to access is generated by PHP, your code shouldn't care how the html is generated – Mark Baker Sep 12 '15 at 12:15
  • Please, specify the authentication protocol supported by the web site – nlips Sep 12 '15 at 12:44
  • Does your PHP handle the POST method or the GET method for the different actions? Your first line suggests GET. If it is supposed to be POST then first remove that ? from your post data, next append two line feeds to your POST, finally call Flush on your stream writer. If the method is supposed to be GET then just include the query string in your request Uri. – Lorek Sep 12 '15 at 14:17

0 Answers0