日期:2014-05-17  浏览次数:20588 次

ASP.NET后台手动调用POST提交并获取返回值,用于短信平台接口
string userid = "lyancoffee";
            string password = "lyancofe";
            string phone = "15921263164";
            string content = "hello world";
            string time = DateTime.Now.ToString();
            string postData = string.Format("id={0}&pwd={1}&to={2}&content={3}&time={4}", userid, password, phone, content, time); // 要发放的数据
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
            HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create("http://service.winic.org:8009/sys_port/gateway"); //发送地址
            objWebRequest.Method = "POST";//提交方式
            objWebRequest.ContentType = "application/x-www-form-urlencoded";
            objWebRequest.ContentLength = byteArray.Length;
            Stream newStream = objWebRequest.GetRequestStream(); // Send the data.
            newStream.Write(byteArray, 0, byteArray.Length); //写入参数
            newStream.Close();
            HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();//获取响应
            StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
            string textResponse = sr.ReadToEnd() + "返回数据"; // 返回的数据

            Response.Write(textResponse);//打印返回值

短信平台

------解决方案--------------------

        public static string PostDataGetHtml(string uri, string postData)
        {
            try
            {
                byte[] data = Encoding.UTF8.GetBytes(postData);

                Uri uRI = new Uri(uri);
                HttpWebRequest req = WebRequest.Create(uRI) as HttpWebRequest;
            &nbs