c#使用HttpWebRequest向指定url Post数据

 

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.yang1.com/test.aspx");
            request.Method="POST";
            request.ContentType = "application/x-www-form-urlencoded";

            byte[] byteArr = Encoding.UTF8.GetBytes("a=test");

            request.ContentLength = byteArr.Length; 
            
            Stream stream0 = request.GetRequestStream();

            stream0.Write(byteArr, 0, byteArr.Length);
            stream0.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream2 = response.GetResponseStream();
            byte[] byteArr2 = new byte[(int)response.ContentLength];
            stream2.Read(byteArr2, 0, (int)response.ContentLength);

            string s = Encoding.UTF8.GetString(byteArr2);
            Response.Write(s);

 

posted on 2012-08-31 09:42  ayao  阅读(328)  评论(0编辑  收藏  举报

导航