Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get HTTP web request to return complete source

I am trying to retrieve source code for a webpage and I'm using HTTPWebRequest but only this is returned:

<script type="text/javascript"> 
    window.location="index.php"; 
</script> 

I used fiddler to get a comparison of chrome getting the webpage and compared it with my what my code retrieves.

Chrome is on the left and VS code on the right

https://i.sstatic.net/Hgk9w.png

What I've noticed is that the content length is no where as large as what chrome gets back. My code content length is usually around 70 bites and chrome gets back 87000 bites usually.

I have tried using Stream, and memory stream. Can someone point me in the right direction?

Here is my function below:

public string GetAllCampaings()
{
    string campaigns = null;
    byte[] result;
    byte[] buffer = new byte[4096];

    HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create("http://magiclampmarketing.com/sms/manage_groups.php");
    httpWebRequest2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    httpWebRequest2.Method = "GET";
    httpWebRequest2.CookieContainer = cookieContainer;
    httpWebRequest2.KeepAlive = true;
    httpWebRequest2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11";
    httpWebRequest2.Referer = "http://magiclampmarketing.com/sms/main.php";
    httpWebRequest2.SendChunked = false;

    WebHeaderCollection myWebHeaderCollection = httpWebRequest2.Headers;
    myWebHeaderCollection.Add("Accept-Language", "en;q=0.8");
    myWebHeaderCollection.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
    myWebHeaderCollection.Add("Accept-Encoding", "gzip,deflate,sdch");

    var sp = httpWebRequest2.ServicePoint;
    var prop = sp.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic);
    prop.SetValue(sp, (byte)0, null);

    using (WebResponse response = httpWebRequest2.GetResponse())
    {
        using (Stream responseStream = response.GetResponseStream())
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                int count = 0;
                do
                {
                    count = responseStream.Read(buffer, 0, count);
                    memoryStream.Write(buffer, 0, count);

                } while (count != 0);

                result = memoryStream.ToArray();
            }
        }
    }

    return campaigns; 
}
like image 544
Keith Drummond Avatar asked Jul 26 '26 00:07

Keith Drummond


1 Answers

Looks like the chrome browser you are using is already logged into that site and has a session cookie. However your code does not highlight how you are passing the session cookie.

From what is seen, it looks like the request from your program is coming back with a response that is redirecting you to login page.

You either have to clarify your take on passing the session cookie. Or accept that your programming is doing what it is expected to do.

like image 98
humblelistener Avatar answered Jul 28 '26 13:07

humblelistener