隐藏

c# 解决不带参数的POST字符串

发布:2014/6/13 9:41:57作者:管理员 来源:本站 浏览次数:1773

            StringBuilder str = new StringBuilder();
            str.Append("<?xml version='1.0' encoding='UTF-8' ?>");
            str.Append("<req>");
            str.Append("<head>");
            str.AppendFormat("<user>{0}</user>", "333333");
            str.AppendFormat("<pwd>{0}</pwd>", "dsafdafdafdafdafdafdafdafd");
            str.AppendFormat("<cmd>{0}</cmd>", "post");
            str.Append("</head>");
            str.Append("<ad>");
            str.AppendFormat("<time_start>{0}</time_start>", DateTime.Now.ToShortDateString().Replace("/", "-"));
            str.AppendFormat("<time_expire>{0}</time_expire>", DateTime.Now.AddDays(2).ToShortDateString().Replace("/", "-"));
            str.AppendFormat("<time_zone>{0}</time_zone>", "9,18");
            str.AppendFormat("<start>{0}</start>", 1);
            str.AppendFormat("<content>{0}</content>", "您可以用 winrar 解压开来安装也可以用虚拟光盘之类的来安装.");


            //str.AppendFormat("<content>{0}</content>", "how do you do.");
            str.Append("</ad>");
            str.AppendFormat("<number>{0}</number>", "150********,155*******,133*******");
            str.Append("</req>");
            textBox2.Text = str.ToString();


            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            // 要注意的这是这个编码方式,还有内容的Xml内容的编码方式
            Encoding encoding = Encoding.GetEncoding("UTF-8");
            byte[] data = encoding.GetBytes(str.ToString());


            // 准备请求,设置参数
            request = WebRequest.Create("http://www.ejk5.com") as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/octet-stream";
            //request.ContentLength = data.Length;


            outstream = request.GetRequestStream();
            outstream.Write(data, 0, data.Length);
            outstream.Flush();
            outstream.Close();
            //发送请求并获取相应回应数据


            response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            instream = response.GetResponseStream();


            sr = new StreamReader(instream, encoding);
            //返回结果网页(html)代码


            string content = sr.ReadToEnd();
            textBox1.Text = content;