Skip to main content
GameDev.net gamedev.net
🔒 Locked

StackOverflowException help

Started by NecoSpes Aug 24, 2008 at 12:36 AM 7 replies 1k views
Original Post
NecoSpes
NecoSpes
I'm pretty sure I know what a StackOverflowException is, I've seen it happen when I put big numbers in a recursive program, but I don't see how it's happening here. It occurs when I call the method Request, around where the comment indicates.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

public class Interweb
{
    public CookieCollection Cookies
    {
        get 
        {
            return Cookies;
        }
        set
        {
            Cookies = value;
        }
    }
    public Interweb()
    { 
    }
    public String Request(String Method, String URL, String Post, String Referer)
    { 
        HttpWebRequest http = (HttpWebRequest)WebRequest.Create(URL);
        http.Timeout = 10000;
        http.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
        http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        http.Referer = Referer;
        http.CookieContainer = new CookieContainer();
        Console.WriteLine("Check");
        if (Cookies != null && Cookies.Count > 0) //StackOverflowException happens hereish
        {
            http.CookieContainer.Add(Cookies);
        }
        Console.WriteLine("Check");
        if (Method == "POST")
        {
            http.Method = "POST";
            byte[] postBuffer = Encoding.GetEncoding(1252).GetBytes(Post);
            http.ContentLength = postBuffer.Length;
            Stream sendPost = http.GetRequestStream();
            sendPost.Write(postBuffer, 0, postBuffer.Length);
            sendPost.Close();
        }
        HttpWebResponse response = (HttpWebResponse)http.GetResponse();
        
        if (response.Cookies.Count > 0)
        {
            if (Cookies == null)
            {
                Cookies = response.Cookies;
            }
            else 
            {
                foreach (Cookie respCookies in response.Cookies)
                { 
                    bool match = false;
                    foreach (Cookie currentCookies in Cookies)
                    {
                        if (respCookies.Name == currentCookies.Name)
                        {
                            currentCookies.Value = respCookies.Value;
                            match = true;
                            break;
                        }
                    }
                    if (!match)
                    {
                        Cookies.Add(respCookies);
                    }
                }
            }
        }
        Encoding enc = Encoding.GetEncoding(1252);
        StreamReader respStream = new StreamReader(response.GetResponseStream(), enc);
        String html = respStream.ReadToEnd();
        return html;
    }
}
bitshifter
bitshifter
How about testing the return value of:
http.CookieContainer = new CookieContainer();
NecoSpes
NecoSpes
It just says System.Net.CookieContainer when I do Console.WriteLine(http.CookieContainer = new CookieContainer());
bitshifter
bitshifter
And what if:
if(CookieContainer == null){   Console.WriteLine("bad cookie container.");}
NecoSpes
NecoSpes
Nothing shows up. I think that it happens at this line:
http.CookieContainer.Add(Cookies);
bitshifter
bitshifter
Does http.CookieContainer.Add(Cookies); return a value?
Have you stepped through the debugger up to this point?
NecoSpes
NecoSpes
Well, I tried the debugger, and it said the exception happened here:
get {      return Cookies; //here}


I'm coming to C# from C++/Java, so I'm not very familiar with properties.

Edit-
It looks like this line causes the exception because it calls Interweb.Cookies.get() like a billion times. How do I fix this?

[Edited by - NecoSpes on August 24, 2008 4:35:25 AM]
Endurion
Endurion
Isn't that property getter just repeatedly calling itself?

You need to use two different names for the actual member and the property name.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
NecoSpes
NecoSpes
Yup that was it, thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.