Page 1 of 1

C#.NET HTTP Headers

Posted: Tue Feb 26, 2008 10:44 am
by 3cmSailorfuku
If anyone's familiar with C#;

Code: Select all

        public class PostUtility
        {
            public static string HttpPost(string uri, string parameters)
            {	
                WebRequest webRequest = WebRequest.Create(uri);
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.Headers.Add("Cookie: xxxxx");
                webRequest.Method = "POST";
                byte[] bytes = Encoding.ASCII.GetBytes(parameters);
                Stream os = null;
                try
                {
                    webRequest.ContentLength = bytes.Length;
                    os = webRequest.GetRequestStream();
                    os.Write(bytes, 0, bytes.Length);
                }
                finally
                {
                    if (os != null)
                    {
                        os.Close();
                    }
                }
                return null;
            }
        }
It does work, however after 2 times Sending the Post Header the tool will freeze, which I cannot explain to myself. I've tried opening the stream already just on pressing start and closing it later, but that didn't helped either.

The original is from
http://en.csharp-online.net/HTTP_Post


And theres no offtopic forum :cry:

Re: C#.NET HTTP Headers

Posted: Tue Feb 26, 2008 11:27 am
by Administrator
There is now ;) If you run this locally, and have it dump to a file rather than to your standard output stream, what is produced(if anything)?

I'll have somebody that knows C# look at this later (assuming I remember). He's sleeping at the moment.

Re: C#.NET HTTP Headers

Posted: Tue Feb 26, 2008 11:43 am
by 3cmSailorfuku
Silly me, forgot to delete the content of the stream x,x

Code: Select all

   try
   { // get the response
      WebResponse webResponse = webRequest.GetResponse();
      if (webResponse == null) 
         { return null; }
      StreamReader sr = new StreamReader (webResponse.GetResponseStream());
      return sr.ReadToEnd ().Trim ();
   }
Appending that solved it. And the code above is a bitch. Literally. I forgot that a class is using new memory on use. :roll: