zoukankan      html  css  js  c++  java
  • http 异步 接收 回传 数据文字和文件流

           public void HttpListenerStar()
            {
                try
                {
                    HttpListener httpListener = new HttpListener();
                    httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
                    httpListener.Prefixes.Add("http://+:8080/");              
                    httpListener.Start();
                    httpListener.BeginGetContext(new AsyncCallback(GetContextCallBack), httpListener);
    
                }
                catch (Exception ex)
                {
                    writeLog(ex.ToString());
                }
            }
    
            private void GetContextCallBack(IAsyncResult ar)
            {
                System.Net.HttpListener listerner = ar.AsyncState as System.Net.HttpListener;
                HttpListenerContext requestContext = listerner.EndGetContext(ar);
                try
                {
                    Thread th = new Thread(new ThreadStart(delegate
                    {
                        HttpListenerDataParsing(requestContext);
                    }));
                    th.Start();
    
                }
                catch (Exception ex)
                {
                    try
                    {
                        requestContext.Response.StatusCode = 500;
                        requestContext.Response.ContentType = "text/html";
                        requestContext.Response.ContentEncoding = Encoding.UTF8;
                        byte[] buffer = System.Text.Encoding.UTF8.GetBytes("System Error");
                        //对客户端输出相应信息.  
                        requestContext.Response.ContentLength64 = buffer.Length;
                        System.IO.Stream output = requestContext.Response.OutputStream;
                        output.Write(buffer, 0, buffer.Length);
                        //关闭输出流,释放相应资源  
                        output.Close();
                    }
                    catch (Exception ee)
                    {
                        writeLog(ee.ToString());
                    }
                }
                listerner.BeginGetContext(new AsyncCallback(GetContextCallBack), listerner);
            }
    
    
           private void HttpListenerDataParsing(HttpListenerContext request)
            {
                byte[] content_to_bytes = null;
                try
                {
                    string RawUrl = request.Request.RawUrl;
                    writeLog(DateTime.Now + "
    " + RawUrl);
                    string refData = "";
                    String ip = request.Request.RemoteEndPoint.Address.ToString();
                    if (ipTable.ContainsKey(ip) || (ip.Length > 10 && ip.Substring(0, 10) == "192.168.1."))
                    {
                        string type = HttpUtility.ParseQueryString(RawUrl.Substring(1, RawUrl.Length - 1))["type"];  //不要问我问什么要减1 ,我特么也不知道为什么加了一斜杠就解析不出来了
                        string time = HttpUtility.ParseQueryString(RawUrl.Substring(1, RawUrl.Length - 1))["time"];
                        string sign = HttpUtility.ParseQueryString(RawUrl.Substring(1, RawUrl.Length - 1))["sign"];
    
                        if (type == "get")
                        {
                           refData= Encoding.UTF8.GetBytes("ok");
                        }
                        else if (type == "down")
                        {
                            if (time != null && time != "" && sign != null && sign != "")
                            {
                                string voiceName = Path.Combine(voicePath, time, sign);
                                if (!File.Exists(voiceName))
                                    voiceName = "1243.txt";
                                using (FileStream reader = new FileStream(voiceName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                                {
                                    request.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(sign, System.Text.Encoding.UTF8));
                                    request.Response.ContentType = "application/octet-stream";
                                    request.Response.StatusCode = 200;
                                    request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                                    request.Response.ContentEncoding = Encoding.UTF8;
                                    request.Response.ContentLength64 = reader.Length;
                                    var output = request.Response.OutputStream;
                                    int length = (int)reader.Length;
                                    byte[] pReadByte = new byte[length];
                                    int read = 0;
                                    //断点发送 在这里判断设置reader.Position即可
                                    while ((read = reader.Read(pReadByte, 0, length)) != 0)
                                    {
                                        output.Write(pReadByte, 0, read);
                                    }
                                    output.Close();
                                    reader.Close();
                                }
                                return;
                            }
    
                        }
                    }
                    else
                    {
                        refData = "Not is iptable";
                    }
                    content_to_bytes = Encoding.UTF8.GetBytes(refData);
                    request.Response.ContentType = "application/json";
                    request.Response.StatusCode = 200;
                    request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                    request.Response.ContentEncoding = Encoding.UTF8;
                    request.Response.ContentLength64 = content_to_bytes.Length;
                    var output1 = request.Response.OutputStream;
                    output1.Write(content_to_bytes, 0, content_to_bytes.Length);
                    output1.Close();
                }
                catch (Exception ex)
                {
                    writeLog(ex.ToString());
                }
    
            }
  • 相关阅读:
    node.js 安装
    spring mvc 表单标签
    配置文件 .properties 的使用。
    angular 参考文档
    bootStrap 教程 文档
    idea 安装 破解方法
    restful 注解 总结 (比较完整的):http://www.xuetimes.com/archives/388 , https://www.cnblogs.com/chen-lhx/p/5599806.html
    apo 简单参考
    jsonUtils&&Json、Xml转换工具Jackson使用
    restful 分风格
  • 原文地址:https://www.cnblogs.com/qc-id-01/p/9922878.html
Copyright © 2011-2022 走看看