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 = ;
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, , 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 + "\r\n" + RawUrl);
string refData = "";
String ip = request.Request.RemoteEndPoint.Address.ToString();
if (ipTable.ContainsKey(ip) || (ip.Length > && ip.Substring(, ) == "192.168.1."))
{
string type = HttpUtility.ParseQueryString(RawUrl.Substring(, RawUrl.Length - ))["type"]; //不要问我问什么要减1 ,我特么也不知道为什么加了一斜杠就解析不出来了
string time = HttpUtility.ParseQueryString(RawUrl.Substring(, RawUrl.Length - ))["time"];
string sign = HttpUtility.ParseQueryString(RawUrl.Substring(, RawUrl.Length - ))["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 = ;
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 = ;
//断点发送 在这里判断设置reader.Position即可
while ((read = reader.Read(pReadByte, , length)) != )
{
output.Write(pReadByte, , 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 = ;
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, , content_to_bytes.Length);
output1.Close();
}
catch (Exception ex)
{
writeLog(ex.ToString());
} }

http 异步 接收 回传 数据文字和文件流的更多相关文章

  1. python asyncio 异步实现mongodb数据转xls文件

    from pymongo import MongoClient import asyncio import xlwt import json class Mongodb_Transfer_Excel( ...

  2. js要怎么接收后端传的excel文件流?

    方法1: 无需js,直接用a标签去接你的输出流 <a href="<你的返回流的Action路径>" >下载</a> 方法2:使用js,前提是你 ...

  3. ffmpeg接收udp输入的h264文件流,推流到rtmp服务器

    ffmpeg -re -f h264 -i udp://192.168.5.49:10002 -vcodec libx264 -f flv rtmp://192.168.5.155/live/1

  4. 12 文件流(Unity3D)

      关于文件操作在脚本中可以通过xml或其他的与文件相关的类中封装的一些方法可读取本地Asset文件夹下的xml文档和json文档到游戏中.但是这样操作文件难免会有拖延,C#提供了一种文件操作的方法, ...

  5. c语言中的文件流

    一.打开和关闭文件 #include int main( void ) { FILE* pReadFile = fopen( "E:\\mytest.txt", "r&q ...

  6. 5个对话框和FileStream:文件流

    1.private void button1_Click(object sender, EventArgs e) { colorDialog1.ShowDialog();//显示颜色选择器 panel ...

  7. 中小学教育缴费----支付宝回传数据.net core 接收中文乱码

    问题描述: 中小学教育缴费,发送账单到家长支付宝,家长支付成功之后,支付宝回传数据,验签的时候失败了,排查之后发现账单名称乱码了.支付宝回传的时候中文传的是GBK编码格式,但是我接收的是%D5˵%A5 ...

  8. [C#]Socket通信BeginReceive异步接收数据何时回调Callback

    原文地址:http://www.cnblogs.com/wangtonghui/p/3277303.html 最近在做服务器压力测试程序. 接触了一段时间Socket异步通讯,发现自己对BeginRe ...

  9. socket 异步接收连接和接收数据

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

随机推荐

  1. python------模块基础【第二部分-time】------

    一.time UTC/GMT:世界时间 本地时间:本地时区时间 python中时间日期格式化符号: %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01- ...

  2. python函数篇0-1

    创建类和对象 面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就是对 “类” 和 “对象” 的使用. 类就是一个模板,模板里可以包含多个函数, ...

  3. OnMouseWheel的通常处理

    BOOL CMainWindow::OnMouseWheel(UINT nFlags, short zDelta, CPoint point) { BOOL bUp = TRUE; int nDelt ...

  4. lua与c的交互(函数专用)

    Lua与C的交互 Lua是一个嵌入式的语言,它不仅可以是一个独立运行的程序,也可以是一个用来嵌入其它应用的程序库. C API是一个C代码与Lua进行交互的函数集,它由以下几部分构成: 1.  读写L ...

  5. 作业6:Java虚拟机类加载机制

    一.概述 1.定义 虚拟机类加载机制:把类的数据从Class文件加载进内存,并对数据作校验.转换解析和初始化,最终形成可被JVM直接使用的Java类型. 2.与C/C++的不同 Java不在编译时进行 ...

  6. STM32F10xxx_异常与中断

    STM32F10xxx_异常与中断 [TOC] 更新记录 version status description date author V1.0 C Create Document 2018.10.2 ...

  7. java三大框架——Struts + Hibernate + Spring

    Struts主要负责表示层的显示 Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作) Hibernate主要是数据持久化到数据库 再用jsp的servlet做网页开发的时候有个 w ...

  8. 乐观锁之版本号机制和CAS

    ---恢复内容开始--- 乐观锁:每次去拿数据的时候,都认为别人不会修改,不会加锁,但在更新的时候会去判断一下,此期间别人有没有更新数据,版本号机制和CAS算法就用到乐观锁,参考了https://bl ...

  9. 第四篇.python的基础

    目录 第四篇.python基础01 1. 变量 2. 常量 3. python变量内存管理 4. 变量的三个特征 5. 花式赋值 6. 注释 7. 数据类型基础 8. 数字类型 9. 字符串类型 10 ...

  10. sql分页查询(2005以后的数据库)和access分页查询

    sql分页查询: select * from ( select ROW_NUMBER() over(order by 排序条件) as rowNumber,* from [表名] where 条件 ) ...