HttpListener通讯成功案例
1、创建WindowsService,如下代码
using System;
using System.Net;
using System.Net.Sockets;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace WindowsService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
public static int TerminalPort = 12800;
public static Thread ListeningThread;
public static int Port;
public static Encoding Encoding;
protected override void OnStart(string[] args)
{
InitialAcceptor(TerminalPort);
}
protected override void OnStop()
{
}
public void DebugTest()
{
Console.WriteLine("StartService...");
InitialAcceptor(TerminalPort);
Console.WriteLine("Terminal Listened on Port=" + TerminalPort);
}
public void InitialAcceptor(int tunnelPort)
{
Port = tunnelPort;
Encoding = Encoding.UTF8;
ListeningThread = new Thread(new ThreadStart(StartListening));
ListeningThread.IsBackground = Environment.OSVersion.Platform != PlatformID.Unix;//
ListeningThread.Start();
}
#region MyRegion
//internal void StartListening()
//{
// try
// {
// while (true)
// {
// try
// {
// listener = new HttpListener();
// listener.Prefixes.Add("http://+:" + Port + "/");
// listener.Start();
// }
// catch
// {
// Thread.Sleep(1000);
// }
// try
// {
// while (true)
// {
// IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
// result.AsyncWaitHandle.WaitOne();
// }
// }
// catch (Exception e)
// {
// }
// }
// }
// catch
// {
// }
//}
#endregion
internal void StartListening()
{
try
{
while (true)
{
try
{
#region MyRegion
//服务端:
//var listener = new HttpListener();
//listener.Prefixes.Add("http://*:8080/");
//listener.Start();
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://+:" + 1234 + "/");
listener.Start();
while (true)
{
IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
result.AsyncWaitHandle.WaitOne();
}
#endregion
}
catch
{
Thread.Sleep(1000);
}
}
}
catch
{
}
}
private void ListenerCallback(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
HttpListenerContext context = listener.EndGetContext(result);
processRequest(context);
}
private void processRequest(HttpListenerContext context)
{
HttpListenerRequest request = context.Request;
try
{
//string url = System.Web.HttpUtility.UrlDecode(request.RawUrl);
string ss = request.QueryString["eid"];
context.Response.ContentEncoding = UTF8Encoding.UTF8;
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
//context.Response.ContentType = "image/jpg";
//context.Response.ContentLength64 = buffer.Length;
var output = context.Response.OutputStream;
byte[] nn = System.Text.Encoding.Default.GetBytes("qqqqqqqqqqqq");
output.Write(nn, 0, nn.Length);
output.Close();
}
catch (Exception e)
{
}
finally
{
}
}
}
}
2、创建控制台,测试启动WindowsService服务
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using WindowsService;
namespace WindowsService1TestConsole
{
class Program
{
static void Main(string[] args)
{
try
{
Service1 service1 = new Service1();
#if DEBUG
service1.DebugTest();
#endif
Thread.CurrentThread.IsBackground = false;
Thread.Sleep(Timeout.Infinite);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.Source);
Console.WriteLine(e.StackTrace);
Console.WriteLine(e.InnerException != null ? e.InnerException.ToString() : "");
Console.ReadLine();
}
}
}
}
3、测试http请求
直接 地址栏输入:http://127.0.0.1:1234/?eid=30
结果:qqqqqqqqqqqq

写测试代码
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();//返回结果
myStreamReader.Close();
myResponseStream.Close();
HttpListener通讯成功案例的更多相关文章
- Socket通讯成功案例
Socket通讯案例 #region 服务端 //int port = 1234; //string host = "127.0.0.1"; //IPAddress ip = IP ...
- 【即时通讯】即时通讯及XMPP概述及…
在讲解XMPP前,我们需要先了解什么是即时通讯技术: * 即时通讯技术(IM - InstantMessaging)支持用户在线实时交谈.当一方需要发送消息时,用户必须打开一个窗口,以便让用户与交流对 ...
- thrift实现js与C#通讯
利用thrift实现js与C#通讯的实例代码 1.为什么要用thrift js C#? 1.1 首先,js 通过 thrift 访问C#,实际上是一种c/s模式.thrift是通信工具,js是客户端, ...
- 细说Web页面与本地电脑通讯
话说在很久很久以前.Web页面与客户的本地电脑Localhost通讯,有两种方式: 1.Flash 2.ActiveX控件 由于Flash本人不是很了解,也给出不了什么示例代码, 对于ActiveX控 ...
- Android HTTP通讯
这里有一个非常棒的http通讯的总结,我看了以后茅塞顿开. 先贴代码: 01 public class Activity1 extends Activity { 02 03 private ...
- 简单搭建http服务器-HttpListener使用
使用HTTPListener可以简单搭建一个Http服务器,对于本地使用很是方面,想起之前使用了WebSocket来与本地网页通讯的例子,也是可以改为使用HTTPListener来做的.看下HTTPL ...
- HTML5笔记:跨域通讯、多线程、本地存储和多图片上传技术
最近做项目在前端我使用了很多新技术,这些技术有bootstrap.angularjs,不过最让我兴奋的还是使用了HTML5的技术,今天我想总结一些HTML5的技术,好记性不如烂笔头,写写文章可以很好的 ...
- (转)利用libcurl和国内著名的两个物联网云端通讯的例程, ubuntu和openwrt下调试成功(四)
1. libcurl 的参考文档如下 CURLOPT_HEADERFUNCTION Pass a pointer to a function that matches the following pr ...
- (转)linux下和云端通讯的例程, ubuntu和openwrt下实验成功(二)
前言: 上节用纯linux的函数实现了和云端通讯, 本节开始利用传说中的神器libcurl 话说一个网络程序员对书法十分感兴趣,退休后决定在这方面有所建树. 于是花重金购买了上等的文房四宝. 一 ...
随机推荐
- springboot06-swagger2 自动化api文档
1.springboot 项目中添加swagger2依赖: <dependency> <groupId>org.springframework.boot</groupId ...
- BN_batch normalization
参考: https://zhuanlan.zhihu.com/p/27938792 做法 设,每个batch输入是 (其中每个 都是一个样本, 是batch size) 假如在第一层后加入Batch ...
- java使用类数组 报错Exception in thread "main" java.lang.NullPointerException
源代码如下: Point[] points=new Point[n];//Point是一个类 for(int i=0;i<n;i++) { System.out.print("请输入x ...
- vue运行说明
1.cd 到demo里面 如:cd vuedemo(项目名) 2.安装依赖: npm install 3.运行项目 npm run dev
- 电路板上的元件R,T,D,C,U
参考链接: https://zhidao.baidu.com/question/194132005.html
- Django REST framework 第二章 Request and Response
此章节开始真正的撰写REST framework的核心代码,介绍一系列必要的建立设计 Request Objects REST framework介绍了一个Request对象用来扩展常规的HttpRe ...
- 【mmall】Guava框架
Guava 简介:http://www.yiibai.com/guava 本项目主要用到了Guava缓存
- Python 包
转载自https://www.cnblogs.com/feeland/p/4401758.html Python 包 包 定义 为了组织好模块,会将多个模块分为包.Python 处理包也是相当方便的. ...
- markdown自动生成侧边栏TOC /目录
http://blog.csdn.net/haleypku/article/details/51226704 此文可以只了解一下概念: http://i5ting.github.io/i5ting_z ...
- awk基本用法
1 简介 awk实质是一种编程语言,基本作用在于查找和替换. 2 基本用法 有文本名称为:awk.txt 内容为: john.wang male 30 021-111111 lucy.yang f ...