之前给公司做打印都是用ActiveX控件,只支持IE浏览器,最近需要支持谷歌,又不想去学谷歌插件编写,于是就用本地启动一个http服务器来供浏览器调用(写成windows服务更好),同事用了都说好(笑)!为了方便大家使用,重新简单的封装了一下,源码下载:点我下载
源码简单的封装了一个webserver(基于httplistener,可以使用Nancy框架代替),可以大致了解HTTP服务器处理的流程:APACHE,NINGX等服务器主要负责响应浏览器HTTP(基于SOCKET)的请求,并将请求转交给JAVA,C#,PHP等语言处理(应该就是启动了一个该语言的虚拟机解析执行对应的代码),再将处理结果返回给浏览器
namespace LocalPrint.Web
{
//本地微型HTTP服务器,可以理解网络请求处理的服务器处理流程
class WebServer
{
HttpListener httpListener;
public string Err;
Dictionary<string, IHttpHandler> handlerMap = new Dictionary<string, IHttpHandler>();
//启动本地HTTP服务器,相当于APACHE,NGINX之类的WEB服务器,接受浏览器发送过来的SOCKET请求
public bool RunWeb(string url)
{
try
{
httpListener = new HttpListener();
httpListener.Prefixes.Add(url);
httpListener.Start();
var th = new Thread(Process);
th.IsBackground = true;
th.Start();
return true;
}
catch (Exception ex)
{
Err = "启动本地服务器出现问题:" + ex.Message;
return false;
}
}
//设置相应路由和相应的处理类
public bool AddHandler(string url,IHttpHandler httpHandler)
{
try
{
handlerMap.Add(url, httpHandler);
return true;
}
catch (Exception ex)
{
Err = "添加路径出错:" + ex.Message;
return false;
}
}
//HTTP数据处理,这一部分相当于C#,JAVA和PHP和其他语言的功能,WEB服务器将请求转发给相应的语言处理,并把结果返回给浏览器
void Process()
{
for (; ; )
{
var cnx = httpListener.GetContext();//获取浏览器请求上下文,串行处理,也可以改成并行
var req = cnx.Request;
var rep = cnx.Response;
rep.ContentEncoding = Encoding.UTF8;
rep.Headers.Add("Access-Control-Allow-Origin", "*");//允许浏览器跨域!非常重要
rep.StatusCode = ;
var ret = "pag not found";
//rep.ContentType = "text";//返回内容,这里为text,ajax里面的请求datatype也需要设置为text或者html,不然会为null
//这一部分其实就是大部分MVC网络框架里的路由部分!这里简单的发送原始文本给handler处理
foreach (var kv in handlerMap)
{
if(System.Text.RegularExpressions.Regex.IsMatch(req.RawUrl,kv.Key))//正则匹配
{
var data = "";
if (req.HttpMethod == "GET")
data = req.RawUrl;//不做任何处理,直接将原始的http请求转发到handler。。。
else
using (var r = new StreamReader(req.InputStream, Encoding.UTF8))
{
data = r.ReadToEnd();
}
ret = kv.Value.Handler(data);
rep.StatusCode = ;//ok
break;
}
}
//返回处理结果给浏览器
using (var w = new StreamWriter(rep.OutputStream, Encoding.UTF8))
{
w.WriteLine(ret);
}
}
} }
}
void Process()
{
for(; ; )
{
var cnx = httpListener.GetContext();
var req = cnx.Request;
var rep = cnx.Response;
rep.Headers.Add("Access-Control-Allow-Origin", "*");
var txt = "";
if (req.HttpMethod == "POST")
{
var fp = "";
using (var r = new StreamReader(req.InputStream, Encoding.UTF8))
{
fp = r.ReadToEnd();
}
var printer = req.QueryString["printer"];
switch (req.Url.LocalPath)
{
case "/printZPP":
printComp1.Print(fp, printer);
break;
case "/printTJBB":
printComp1.PrintBB(fp, printer);
break;
case "/printQD":
printComp1.PrintQd(fp, printer);
break;
case "/printAll":
printComp1.UniversalPrint(fp, printer);
break;
default:
//rep.StatusCode = 404;
txt = "";
break;
}
}
else
{
switch (req.Url.LocalPath)
{
case "/GetPrintNames":
txt = printComp1.GetPrinterNames();
break;
case "/GetPrinter":
{
var id = req.QueryString["id"];
txt = printComp1.GetLocalPrinter(id);
}
break;
case "/SetPrinter":
{
var id = req.QueryString["id"];
var printer = req.QueryString["printer"];
printComp1.SetLocalPrinter(id, printer);
}
break;
default:
txt = printComp1.GetPrinterNames();
//rep.StatusCode = 404;
txt = "";
break;
}
} using (var w = new StreamWriter(rep.OutputStream, Encoding.UTF8))
{
w.WriteLine(txt);
}
}
}
HTTP处理接口,PrintHandler派生此接口处理打印任务,也可以派生接口执行其他本地任务,只是简单的处理get和post文本,没有具体的参数解析,需要类自行解析。。。
//类似于MVC框架的ACTION或者CONTROLER,可派生此接口实现其他操作
public interface IHttpHandler
{
string Handler(string txt);//简单的处理文本消息
}
IPrint打印接口,派生此接口执行具体打印任务,打印格式这块可以封装出一整套的处理代码(常用的标签,表格打印和格式设置),有空再处理了
//打印接口,需要打印格式,派生此接口即可
public interface IPrint
{
bool Print(Graphics g);//返回值为是否还有打印需要打印的页内容,即是否打印结束
}
启动本地服务器后,浏览器直接访问或ajax跨域访问本地服务器即可调用本地处理服务!
$.ajax({
url : "http://localhost:23333/print",
type : "POST",
contentType: "application/json;charset=utf-8",
data : "{'name':'大萝卜卜','age':32,'sex':true}",
dataType : "text",
success : function(result) {
if (result == "ok") {
alert("打印成功!");
} else {
alert("打印出错:"+result )
}
},
error:function(msg){
aler('Error:'+msg);
}
})
本地测试
var web = new WebClient();
var txt = textBox1.Text;
byte[] bytearray = Encoding.UTF8.GetBytes(txt);
txt = Convert.ToBase64String(bytearray);
txt = System.Web.HttpUtility.UrlEncode(txt, Encoding.UTF8);//必须经过url编码
web.Encoding = Encoding.UTF8;
web.Headers.Add("ContentType", "application/x-www-form-urlencoded"); try
{
var ret = web.UploadString("http://127.0.0.1:1234", "POST", "args=" + txt);
textBox2.Text = Encoding.UTF8.GetString(Convert.FromBase64String(ret)); }
catch (WebException ex)
{
var rsp = ex.Response as HttpWebResponse;
if(rsp !=null)
{
var code = (int)rsp.StatusCode;
textBox2.Text = code + "\r\n";
var stream = rsp.GetResponseStream();
using (var s = new StreamReader(stream))
{
textBox2.Text += s.ReadToEnd();
}
}
else
{
textBox2.Text = ex.Message; }
}
为了发表博客,花了2个小时,把代码整理,简单的封装成接口,平常写代码哪有这么费事,分分钟写完交差了事(意大利面条,笑),写文章不易,点个赞吧。