Jump to content
  • Advertisement
Sign in to follow this  
d000hg

Problems making HTTP requests from C#

This topic is 3793 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wrote some C# code to try and login to a site, and then read a page from it. It doesn't work properly but I can't tell why... I know the two URLs I pass are good because if I paste them manually into FF/IE, I am able to view the login-required page.I used Fiddler to inspect requests/responses from my app and from IE7, it's obvious a lot more is being sent/received from IE7 but I have no idea what to do about this or what it means. Code (with private information removed) follows:
    class Program
    {
        String userName;
        String password;
        String site;
        CookieCollection cookies;

        static void Main(String[] cmdArgs)
        {
            Program P = new Program(cmdArgs[0], cmdArgs[1], cmdArgs[2]);
            P.login();
            P.ReadHand(cmdArgs[3]);
        }

        public Program(string userName, string password, string site)
        {
            this.userName = userName; this.password = password; this.site = site;
        }

        void login()
        {
            string uri = "http://www."+site+".com/account/login/login";
            string args="username="+userName+"&password="+password+"&sAction=login";
            uri += "?"+args;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.KeepAlive = false;
            request.ProtocolVersion=HttpVersion.Version11;
            request.Method = "GET";
            request.CookieContainer = new CookieContainer();
            request.KeepAlive = true;
            request.ContentType = "text";

            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            String content = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //Console.WriteLine(content);
            Console.WriteLine("Status: "+response.StatusCode);
            Console.WriteLine("Headers: ");
            Console.WriteLine(response.Headers.ToString());
            foreach (Cookie cookie in response.Cookies)
            {
                Console.WriteLine("Cookie: " + cookie.Name + " = " + cookie.Value);
            }
            File.WriteAllText("login.html", content, Encoding.ASCII);
            Console.ReadLine();
            cookies = response.Cookies;
            response.Close();
        }

        void ReadHand(string hand)
        {
            string uri = "http://www." + site + ".com/account/handhistory/?sRoundReference=" + hand;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Method = "GET";
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(this.cookies);

            request.ContentType = "text";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            String content = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //Console.WriteLine(content);
            Console.WriteLine("Status: " + response.StatusCode);
            Console.WriteLine("Headers: ");
            Console.WriteLine(response.Headers.ToString());
            foreach (Cookie cookie in response.Cookies)
            {
                Console.WriteLine("Cookie: " + cookie.Name + " = " + cookie.Value);
            }
            Console.ReadLine();
            File.WriteAllText(hand + ".html", content, Encoding.ASCII);
            response.Close();
        }

}



[Edited by - d000hg on May 29, 2008 4:05:24 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hi,

I'd guess that the account/login/login expects a POST instead of a GET with query string.

Just a guess, though.

Regards,
Andre

Share this post


Link to post
Share on other sites
Nope, GET is fine. If I actually take the URL the app is using and stick that into the address bar of IE/FF, it works.

Share this post


Link to post
Share on other sites

Can you give some more details about how it's not working properly? Are you not getting back anything at all, or is it just not logging in? Two things I noticed in the login() function (wild guesses!):


request.KeepAlive = false;
...
request.ProtocolVersion=HttpVersion.Version11;
...
request.KeepAlive = true;


I *think* you might want to leave the keepalives out, since they reportedly are incompatible with HTTP 1.1 (see here). I had some vaguely related problems a while back retrieving pages with Java and actually having the server send a Connection: Close header on the page I was retrieving fixed it. It's a bit of voodoo, but maybe that'd help here too.


request.ContentType = "text";


I don't think "text" by itself is a valid mime type, so the server might be getting confused over that. I don't know if its actually required to set this on the request (you might want to try leaving it out), but if you really need to set it you could use something like "text/plain" or "text/plain; charset=utf-8". Charset is optional, but IE uses UTF8 by default, so it might be worth a shot. You may need to convert the URL to the proper encoding if you use the charset option.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!