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 = ;
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 异步 接收 回传 数据文字和文件流的更多相关文章
- python asyncio 异步实现mongodb数据转xls文件
from pymongo import MongoClient import asyncio import xlwt import json class Mongodb_Transfer_Excel( ...
- js要怎么接收后端传的excel文件流?
方法1: 无需js,直接用a标签去接你的输出流 <a href="<你的返回流的Action路径>" >下载</a> 方法2:使用js,前提是你 ...
- 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
- 12 文件流(Unity3D)
关于文件操作在脚本中可以通过xml或其他的与文件相关的类中封装的一些方法可读取本地Asset文件夹下的xml文档和json文档到游戏中.但是这样操作文件难免会有拖延,C#提供了一种文件操作的方法, ...
- c语言中的文件流
一.打开和关闭文件 #include int main( void ) { FILE* pReadFile = fopen( "E:\\mytest.txt", "r&q ...
- 5个对话框和FileStream:文件流
1.private void button1_Click(object sender, EventArgs e) { colorDialog1.ShowDialog();//显示颜色选择器 panel ...
- 中小学教育缴费----支付宝回传数据.net core 接收中文乱码
问题描述: 中小学教育缴费,发送账单到家长支付宝,家长支付成功之后,支付宝回传数据,验签的时候失败了,排查之后发现账单名称乱码了.支付宝回传的时候中文传的是GBK编码格式,但是我接收的是%D5˵%A5 ...
- [C#]Socket通信BeginReceive异步接收数据何时回调Callback
原文地址:http://www.cnblogs.com/wangtonghui/p/3277303.html 最近在做服务器压力测试程序. 接触了一段时间Socket异步通讯,发现自己对BeginRe ...
- socket 异步接收连接和接收数据
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
随机推荐
- Linux就该这么学——新手必须掌握的命令之常用的系统工作命令
echo命令 含义:echo命令用于在终端输出字符串或变量提取后的值,格式为 : echo [字符串|$变量] 示例: 将”Linuxprobe.com”输出到终端屏幕的命令为: [root@linu ...
- php+lottery.js制作九宫格抽奖实例
php+lottery.js制作九宫格抽奖实例,本抽奖功能效果表现好,定制方便简单,新手学习跟直接拿来用都非常不错,兼容IE.火狐.谷歌等浏览器. 引入抽奖插件lottery.js <scrip ...
- 怎样理解DOM
一句话总结: DOM 是一个 js 对象. 他可以赋予 js 控制 html 文档的能力. 全称: Document Object Model. DOM 的最小组成单位是: 节点 , 节点有7种类型 ...
- linux常用的bash指令
文本处理 awk sed grep sort uniq cat cut echo fmt tr nl egrep fgrep wc 进程监视 ps top htop atop lsof 网络 nmap ...
- mysql 8.x 登陆提示 Access denied for user 'root'@'localhost' (using password: YES)
第一步:修改 /etc/mysql/my.cnf. 在[mysql]下添加skip-grant-table:重启. 第二步:通过mysql命令登陆: flush privileges; use mys ...
- Ubuntu系统开机后不能正常使用——问题解决记录
1.开机后桌面内容没了,搜狗输入法不能使用了,终端不能打开了 问题原因:上次关机前为了解决解压文件中文乱码问题,在/etc/profile末尾加了如下两行:(但事实上如下两行根本不能解决中文乱码问题) ...
- 和 Python 2.x 说再见!项目移到python3
如果你仍在使用 2.x,那么是时候将你的代码移植到 Python 3 了. 在技术的长河中,软件.工具.系统等版本的迭代本是常事,但由于使用习惯.版本的兼容性.易用性等因素,很多用户及开发者在使用或做 ...
- 深入探讨java的类加载器
类加载器是 Java 语言的一个创新,也是 Java 语言流行的重要原因之一.它使得 Java 类可以被动态加载到 Java 虚拟机中并执行.类加载器从 JDK 1.0 就出现了,最初是为了满足 Ja ...
- HTML5之客户端存储(Storage)
关于客户端存储技术 storage 一.关于客户端(浏览器)存储技术 浏览器的存储技术第一个能想到的应该就是cookie,关于cookie本身出现的初衷是为了解决客户端识别,可存储信息量小(4k左右) ...
- nginx 配置反向代理根目录到其他服务器
location /detail/json { if ( $uri = "/detail/json" ) { rewrite "/detail/json" /i ...