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. php链接redis (带密码)常用的redis方法

    连接redis $redis = new Redis(); $redis->connect($host, $port); $redis->auth('my pass'); //密码验证 常 ...

  2. curl post请求封装

    /* POST /servlet/ICBCCMPAPIReqServlet?userID=jyi.y.1001&PackageID=201807311347539185&SendTim ...

  3. HTML DOM focus() 方法

    目录 HTML DOM focus() 方法 实例 定义和使用 浏览器支持 语法 参数 技术描述 更多实例 实例 实例 HTML DOM focus() 方法 实例 为 <a> 元素设置焦 ...

  4. Codeforces 1244D. Paint the Tree

    传送门 首先如果某个点的度数大于 $2$ 那么显然无解 然后考虑点的度数小于等于 $2$ 的情况 发现其实是一条链 一旦确定了链开头的两个点,后面的点的颜色都可以通过之前的点推出 所以直接枚举即可 # ...

  5. linux 下vim 开发环境配置(通用所有编程语言)

    1.下载 http://www.iterm2.com/ 2.oh-my-zsh curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master ...

  6. Phoenix设置联合主键

    1例如 create table test12(email varchar not null,id integer not null,name varchar not null,age integer ...

  7. centos 7 源代码 mysql-5.7.2 安装

    CENTOS MYSQL 5.7 下载MySQL 5.7 https://dev.mysql.com/downloads/mysql/5.7.html#downloads cd /usr/local/ ...

  8. linux学习笔记七

    #文件权限很重要,有些时候删除和新建文件没有权限根本操作不了,linux一切皆是文件,所以必须得了解下权限了. 文件的一般权限 简单的ls -ld 命令就能看到权限,dr-xr-x---补全应该是dr ...

  9. C++ 内联函数 摘自 C++ 应用程序性能优化

    内联函数 在C++语言的设计中,内联函数的引入可以说完全是为了性能的考虑.因此在编写对性能要求比较高的C++程序时,非常有必要仔细考量内联函数的使用. 所谓"内联",即将被调用函数 ...

  10. CNN for NLP

    卷积神经网络在自然语言处理任务中的应用.参考链接:Understanding Convolutional Neural Networks for NLP(2015.11) Instead of ima ...