DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

HTTP Post

06.20.2007
| 2673 views |
  • submit to reddit
        // description of your code here

            HttpWebRequest  request = (HttpWebRequest)WebRequest.Create(URL);

            byte[]    postDataBytes = Encoding.ASCII.GetBytes(postData);
            request.AllowAutoRedirect = true;

            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = postDataBytes.Length;
            request.Referer = URL;
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4";
            request.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            request.Headers.Add("Accept-Language","en-us,en;q=0.5");
            request.Headers.Add("Accept-Encoding","gzip,deflate");
            request.Headers.Add("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            
            if (cookie != "")
                request.Headers.Add("Cookie",cookie);

            Stream requestStream  = request.GetRequestStream();

            requestStream.Write(postDataBytes, 0 ,postDataBytes.Length);
            requestStream.Close();
	    
	    request.CookieContainer = new CookieContainer();
            // Get response
            resp = (HttpWebResponse)request.GetResponse();
            StreamReader responseReader =  new StreamReader(resp.GetResponseStream());