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 ...
随机推荐
- php栈的定义和出栈、入栈的实现
栈是线性表的一种,他的特点是后入先出,可以这么理解,栈就像一个存东西的盒子,先放进去的在最底层,后放进去的在上层,因为上层的东西把底层的东西压住了,下层的想要出去就必须把上层的先拿开才行. 定义:栈是 ...
- 安装laravel框架
方式一:Windows版本通过composer来下载安装laravel框架 一:laravel是php的一个web框架.laravel框架安装主要依赖composer工具,本经验就介绍一下怎么在win ...
- 【Havel 定理】Degree Sequence of Graph G
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2454 [别人博客粘贴过来的] 博客地址:https://www.cnblogs.com/debug ...
- http请求之of_ordering_json
//Public function of_ordering_json (string as_json,ref string as_jsons[]) returns integer //string a ...
- 解决Spring Boot集成Shiro,配置类使用Autowired无法注入Bean问题
如题,最近使用spring boot集成shiro,在shiroFilter要使用数据库动态给URL赋权限的时候,发现 @Autowired 注入的bean都是null,无法注入mapper.搜了半天 ...
- asp.net 12 AJAX
Javascript:ajax Ajax:get <%@ Page Language="C#" AutoEventWireup="true" CodeBe ...
- 快递100API
url:http://www.kuaidi100.com/query 拼接参数: 参数名称 参数取值 参数类型 type 快递码,请参考快递100码 String postid 快递单号 String ...
- aipai服务架构
概述 业务服务器30+ 1.根据业务不同,有四个主入口,负责负载均衡. 2.主要是业务分离,防止宕机影响所有业务. 3.nginx反向代理,保证每个业务至少有两个服务. redis集群12台 主要使用 ...
- ORA-01145: offline immediate disallowed unless media recovery enabled问题解决
ORA-01145: offline immediate disallowed unless media recovery enabled (随记,后续整理) 数据库只有在归档模式下才能够直接对数据文 ...
- 02-【servlet】
1.什么是Servlet Servlet是JavaWeb的三大组件之一[Servlet,Filter,Listener],它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Se ...