服务器端发送HTTP请求 【2016-04-08 17:43更新】

服务器端发送HTTP请求

.NET

337 2016/4/8 17:43

服务器端代码:

        #region 服务端发送POST请求

        private string url = "http://127.0.0.1/LocationService/location";

        private string parameter = "plateNo=plateNo&simNo=simNo&longitude=123&latitude=123&speed=123&mileage=012&course=course&sendtime=1460087013";

        //服务端发送POST请求

        private string httpPost(string url, string parameter)

        {

 

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

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";

            request.Accept = "*/*";

            request.Timeout = 10000;

            request.AllowAutoRedirect = false;

            StreamWriter requestStream = null;

            WebResponse response = null;

            string responseStr = null;

            try

            {

                requestStream = new StreamWriter(request.GetRequestStream());

                requestStream.Write(parameter);

                requestStream.Close();

                response = request.GetResponse();

                if (response != null)

                {

                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

                    responseStr = reader.ReadToEnd();

                    reader.Close();

                }

            }

            catch (Exception exception)

            {

                return exception.Message;

            }

            finally

            {

                request = null;

                requestStream = null;

                response = null;

            }

            return responseStr;

        }

        

        #endregion

客户端长轮询:


<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>index</title>

    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>

    <script>

        $(function () {

            setInterval(function() {

                $.ajax({

                    url: "http://127.0.0.1/LocationService/location",

                    type: "POST",

                    data: {

                        name: "XiaoMing",

                        age: "18"

                    },

                    error: function () {

                        alert("内部错误");

                    },

                    success: function (data) {

                        $("#post").append("POST:" + data);

                    }

                });

                $.ajax({

                    url: "http://127.0.0.1/LocationService/location?name=XiaoMing&age=18",

                    type: "GET",

                    data: {},

                    error: function() {

                        alert("内部错误");

                    },

                    success: function(data) {

                        $("#get").append("GET:" + data);

                    }

                });

            }, 15000);

            

        });

    </script>

</head>

<body>

    <div id="get"></div>

    <div id="post"></div>

</body>

</html>





上一篇 下一篇
music cover

歌曲名称

作者

00:00/00:00

歌名 歌手 时长