TOP

HttpListener 实现小型web服务器(二)
2019-09-03 03:42:44 】 浏览:121
Tags:HttpListener 实现 小型 web 服务器

at("<HTML><BODY> {0}</BODY></HTML>", DateTime.Now); byte[] buff = Encoding.UTF8.GetBytes(responseString); // 输出回应内容 System.IO.Stream output = response.OutputStream; output.Write(buff, 0, buff.Length); // 必须关闭输出流 output.Close(); } } }

 

可通过网页直接访问。

程序访问方法

string httpUrl = System.Configuration.ConfigurationManager.AppSettings["url"];// http://localhost:8820/
            WebRequest webRequest = WebRequest.Create(httpUrl);
            webRequest.ContentType = "text/xml; charset=utf-8";
            webRequest.Method = "POST";
            string responseString = string.Format("<HTML><BODY> {0}</BODY></HTML>", DateTime.Now);
            //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            using (Stream requestStream = webRequest.GetRequestStream())
            {
                byte[] paramBytes = Encoding.UTF8.GetBytes(responseString);
                requestStream.Write(paramBytes, 0, paramBytes.Length);
            }

            //响应
            WebResponse webResponse = webRequest.GetResponse();
            using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
            {
                string result = "";
                result = myStreamReader.ReadToEnd();
            }

JSON数据传输方法

//data未数据对象
string str = JsonConvert.SerializeObject(data);
string res = Post(httpUrl, str);



 public static string Post(string url, string json)
        {
            string st;
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //创建请求
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.Method = "POST"; //请求方式为post
                request.AllowAutoRedirect = true;
                request.MaximumResponseHeadersLength = 1024;
                request.ContentType = "application/json";
                //request.ContentType = "text/xml; charset=utf-8";
                byte[] jsonbyte = Encoding.UTF8.GetBytes(json);
                Stream postStream = request.GetRequestStream();
                postStream.Write(jsonbyte, 0, jsonbyte.Length);
                postStream.Close();
                //发送请求并获取相应回应数据    
                HttpWebResponse res = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                st = sr.ReadToEnd();
            }
            catch (WebException ex)
            {
                //Log4netHelper.WriteErrorLog(url, ex);
                st = null;
            }

            return st;
        }

 

请关注公众号获取更多资料


HttpListener 实现小型web服务器(二) https://www.cppentry.com/bencandy.php?fid=106&id=249609

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇abp(net core)+easyui+efcore实现.. 下一篇使用Rider中搭建specflow+xunit+s..