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通讯成功案例的更多相关文章

  1. Socket通讯成功案例

    Socket通讯案例 #region 服务端 //int port = 1234; //string host = "127.0.0.1"; //IPAddress ip = IP ...

  2. 【即时通讯】即时通讯及XMPP概述及…

    在讲解XMPP前,我们需要先了解什么是即时通讯技术: * 即时通讯技术(IM - InstantMessaging)支持用户在线实时交谈.当一方需要发送消息时,用户必须打开一个窗口,以便让用户与交流对 ...

  3. thrift实现js与C#通讯

    利用thrift实现js与C#通讯的实例代码 1.为什么要用thrift js C#? 1.1 首先,js 通过 thrift 访问C#,实际上是一种c/s模式.thrift是通信工具,js是客户端, ...

  4. 细说Web页面与本地电脑通讯

    话说在很久很久以前.Web页面与客户的本地电脑Localhost通讯,有两种方式: 1.Flash 2.ActiveX控件 由于Flash本人不是很了解,也给出不了什么示例代码, 对于ActiveX控 ...

  5. Android HTTP通讯

    这里有一个非常棒的http通讯的总结,我看了以后茅塞顿开. 先贴代码: 01 public class Activity1 extends Activity { 02   03     private ...

  6. 简单搭建http服务器-HttpListener使用

    使用HTTPListener可以简单搭建一个Http服务器,对于本地使用很是方面,想起之前使用了WebSocket来与本地网页通讯的例子,也是可以改为使用HTTPListener来做的.看下HTTPL ...

  7. HTML5笔记:跨域通讯、多线程、本地存储和多图片上传技术

    最近做项目在前端我使用了很多新技术,这些技术有bootstrap.angularjs,不过最让我兴奋的还是使用了HTML5的技术,今天我想总结一些HTML5的技术,好记性不如烂笔头,写写文章可以很好的 ...

  8. (转)利用libcurl和国内著名的两个物联网云端通讯的例程, ubuntu和openwrt下调试成功(四)

    1. libcurl 的参考文档如下 CURLOPT_HEADERFUNCTION Pass a pointer to a function that matches the following pr ...

  9. (转)linux下和云端通讯的例程, ubuntu和openwrt下实验成功(二)

    前言: 上节用纯linux的函数实现了和云端通讯, 本节开始利用传说中的神器libcurl 话说一个网络程序员对书法十分感兴趣,退休后决定在这方面有所建树. 于是花重金购买了上等的文房四宝.    一 ...

随机推荐

  1. [笔记]New in Chrome 66

    原文 CSS Typed Object Model 使用CSS object model,返回的一切都是字符串 el.style.opacity = 0.3; console.log(typeof e ...

  2. luogu 2216 理想的正方形 单调队列(其实没有DP)

    #include<bits/stdc++.h> using namespace std; ; ; int a,b,n; int g[A][A],q[A][N],Q[A][N]; int h ...

  3. JavaScript面试技巧(一):基础知识

    1.变量类型和计算 变量类型:值类型.引用类型.typeof运算符. 变量计算:字符串拼接.==运算符.if语句.逻辑运算符 2.原型和原型链 构造函数 5个原型规则 3.作用域和闭包-执行上下文 4 ...

  4. PHP入门知识

    一.搭建开发环境 想要使用一门后端语言,当然是要先搭建开发环境,模拟出服务器环境,不然怎么体现出后端,所以就先大众使用使用的Apache.Mysql,如果不想那么多折腾,建议直接使用xampp或者wa ...

  5. python之接口开发基础知识

    一.开发接口的作用 1.mock 服务:在别的接口没有开发完成的时候可以模拟一些接口以便测试已经开发完成的接口,例如假的支付接口,模拟支付成功.支付失败. 2.了解接口是如何实现的:数据交互.数据返回 ...

  6. 【blog】用emoji-java解决Emoji存储MySQL乱码问题

    emoji-java 核心方法: EmojiParser.parseToAliases(string); 将表情符号转为字符 EmojiParser.parseToUnicode(string); 将 ...

  7. telnet能通但是ping不通

    以前本人以为Telnet通 ping一定也是通的, telnet能通,表示两台计算机之间建立了连接通道.理论上是能ping通的.如果不能ping通,可能的原因是对方主机关闭了ping回显,或者是对方的 ...

  8. python小练习---TCP服务器端

    针对于上一篇分享python小练习---TCP客户端 http://www.cnblogs.com/zhaijiahui/p/6926197.html我继续按书中内容,向下进行这里需要强调一个事py3 ...

  9. Spring重温(二)--Spring JavaConfig

    1.从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中.但是,仍然允许使用经典的XML方式来定义bea ...

  10. php编程 之 php基础三

    1,php里的while循环 循环执行代码块指定的次数,或者当指定的条件为真时循环执行代码块. while实例: <html> <body> <?php $i=1; wh ...