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 ...
随机推荐
- 虚拟机(Vmware)安装ubuntu18.04和配置调整(二)
二.配置修改 1.修改语言环境(settings->Region & Language) 选中中文简体(Chinese(simplified)),点击Apply 中文简体语言安装完成后, ...
- python基础(十二)--模块
模块的导入方式 import os 调用时os.rename from os import rename #只导入的特定功能 调用时rename() from asynico.events impo ...
- docker-Overlay原生网络
节点1/键值存储:192.168.50.130 :192.168.50.131 1.下载Consul二进制包并启动 wget https://releases.hashicorp.com/consul ...
- nginx-host
下载nginx镜像 docker pull docker.io/nginx:latest 由于calico网络不支持http协议,所以即使你在iptables中配置了nat路由,将访问宿主机80端口的 ...
- C#面向对象15 多态
多态 概念:让一个对象能够表现出多种的状态(类型) 实现多态的3种手段:1.虚方法 2.抽象类 3.接口 1.虚方法 步骤:1.将父类的方法标记为虚方法,使用关键字 virtual,这个函数可以被子类 ...
- python numpy 的用法——diag函数
当 np.diag(array) 中 array是一个1维数组时,结果形成一个以一维数组为对角线元素的矩阵 array是一个二维矩阵时,结果输出矩阵的对角线元素
- S=a+aa+aaa...(js)
/*求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字. 例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制.*/ let readline = ...
- 禁用div元素(不可点击)
style="pointer-events: none;" 可以封装使用 .disable { pointer-events: none; } //禁用 $.fn.disable ...
- 销售订单(SO)-API-登记销售订单
登记销售订单可以在新增订单的时候就登记:并不是去修改 flow_status 为booked,而是赋值action request:就下面两句 l_action_request_tbl(l_actio ...
- c#传入类名插入多条数据
public int Insert<T>(IReadOnlyCollection<T> models) where T : class, new() { int sucess ...