用C#写了一个简单web服务器,处理get请求后返回json,客户端通过浏览器发送GET到服务器,服务器根据GET中的参数做对应操作后答复客户端!请问如何操作?比如就是switch中的各个方法,answer=true的时候答复callback([cmd,{"Result":"True"}]),失败返回callback([cmd,{"Result":"False"}])
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace WK
{
class HttpServer
{
private TcpListener myListener;
#region 构造方法
public HttpServer()
{
try
{
//开始兼听8000端口
myListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8000);
myListener.Start();
//同时启动一个兼听进程 StartListen
Thread th = new Thread(new ThreadStart(StartListen));
th.Start();
}
catch (Exception)
{
throw;
}
}
#endregion
void StartListen()
{
int httpIndex = 0;
int cmd; //控制命令
string ip; //终端IP
int wxcode; //微信验证码
string msg; //发送消息内容
string addSong; //点歌歌曲信息
string insertSong; //置顶歌曲信息
while (true)
{
//接受新连接
Socket mySocket = myListener.AcceptSocket();
if (mySocket.Connected)
{
Byte[] bReceive = new Byte[1024];
int i = mySocket.Receive(bReceive, bReceive.Length, 0);
//转换成字符串
string sBuffer = Encoding.UTF8.GetString(bReceive);
//只处理"GET"请求类型
if (sBuffer.Substring(0, 3) != "GET")
{
mySocket.Close();
continue;
}
// 查找 "HTTP" 的位置
httpIndex = sBuffer.IndexOf("HTTP", 1);
//获取到参数
string result = sBuffer.Substring(6, httpIndex - 7);
//获取参数值
NameValueCollection col = GetQueryString(result);
cmd = Convert.ToInt32(col["cmd"]);
ip = col["tip"];
wxcode = Convert.ToInt32(col["wxcode"]);
msg = col["msg"];
addSong = col["addSong"];
insertSong = col["insertSong"];
FaSong send = new FaSong(ip);
bool answer;
#region 根据参数结果发送播控接口
switch (cmd)
{
case 0:
answer = send.Bind(wxcode);
break;
case 1:
answer = send.Repeat();
break;
case 2:
answer = send.Next();
break;
case 3:
answer = send.ZT();
break;
case 4:
answer = send.ZT();
break;
case 5:
answer = send.YCBC();
break;
case 6:
answer = send.YCBC();
break;
case 7:
answer = send.VolCut();
break;
case 8:
answer = send.VolAdd();
break;
case 9:
answer = send.MicCut();
break;
case 10:
answer = send.MicAdd();
break;
case 12:
answer = send.Add(addSong);
break;
case 13:
answer = send.GZ();
break;
case 14:
answer = send.KS();
break;
case 15:
answer = send.HC();
break;
case 16:
answer = send.DC();
break;
case 17:
answer = send.RH();
break;
case 18:
answer = send.ML();
break;
case 19:
answer = send.DG();
break;
case 20:
answer = send.SQ();
break;
case 21:
answer = send.Service();
break;
case 23:
answer = send.Content(msg);
break;
case 26:
answer = send.Insert(insertSong);
break;
default:
answer = false;
break;
}
#endregion
}
mySocket.Close();
}
}
/// <summary>
/// 将字符串解析转换为名值集合.
/// </summary>
/// <param name="queryString">字符串</param>
/// <returns></returns>
NameValueCollection GetQueryString(string queryString)
{
NameValueCollection result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
if (!string.IsNullOrEmpty(queryString))
{
int count = queryString.Length;
for (int i = 0; i < count; i++)
{
int startIndex = i;
int index = -1;
while (i < count)
{
char item = queryString[i];
if (item == '=')
{
if (index < 0)
{
index = i;
}
}
else if (item == '&')
{
break;
}
i++;
}
string key = null;
string value = null;
if (index >= 0)
{
key = queryString.Substring(startIndex, index - startIndex);
value = queryString.Substring(index + 1, (i - index) - 1);
}
else
{
key = queryString.Substring(startIndex, i - startIndex);
}
result[key] = value;
if ((i == (count - 1)) && (queryString[i] == '&'))
{
result[key] = string.Empty;
}
}
}
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace WK
{
class HttpServer
{
private TcpListener myListener;
#region 构造方法
public HttpServer()
{
try
{
//开始兼听8000端口
myListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8000);
myListener.Start();
//同时启动一个兼听进程 StartListen
Thread th = new Thread(new ThreadStart(StartListen));
th.Start();
}
catch (Exception)
{
throw;
}
}
#endregion
void StartListen()
{
int httpIndex = 0;
int cmd; //控制命令
string ip; //终端IP
int wxcode; //微信验证码
string msg; //发送消息内容
string addSong; //点歌歌曲信息
string insertSong; //置顶歌曲信息
while (true)
{
//接受新连接
Socket mySocket = myListener.AcceptSocket();
if (mySocket.Connected)
{
Byte[] bReceive = new Byte[1024];
int i = mySocket.Receive(bReceive, bReceive.Length, 0);
//转换成字符串
string sBuffer = Encoding.UTF8.GetString(bReceive);
//只处理"GET"请求类型
if (sBuffer.Substring(0, 3) != "GET")
{
mySocket.Close();
continue;
}
// 查找 "HTTP" 的位置
httpIndex = sBuffer.IndexOf("HTTP", 1);
//获取到参数
string result = sBuffer.Substring(6, httpIndex - 7);
//获取参数值
NameValueCollection col = GetQueryString(result);
cmd = Convert.ToInt32(col["cmd"]);
ip = col["tip"];
wxcode = Convert.ToInt32(col["wxcode"]);
msg = col["msg"];
addSong = col["addSong"];
insertSong = col["insertSong"];
FaSong send = new FaSong(ip);
bool answer;
#region 根据参数结果发送播控接口
switch (cmd)
{
case 0:
answer = send.Bind(wxcode);
break;
case 1:
answer = send.Repeat();
break;
case 2:
answer = send.Next();
break;
case 3:
answer = send.ZT();
break;
case 4:
answer = send.ZT();
break;
case 5:
answer = send.YCBC();
break;
case 6:
answer = send.YCBC();
break;
case 7:
answer = send.VolCut();
break;
case 8:
answer = send.VolAdd();
break;
case 9:
answer = send.MicCut();
break;
case 10:
answer = send.MicAdd();
break;
case 12:
answer = send.Add(addSong);
break;
case 13:
answer = send.GZ();
break;
case 14:
answer = send.KS();
break;
case 15:
answer = send.HC();
break;
case 16:
answer = send.DC();
break;
case 17:
answer = send.RH();
break;
case 18:
answer = send.ML();
break;
case 19:
answer = send.DG();
break;
case 20:
answer = send.SQ();
break;
case 21:
answer = send.Service();
break;
case 23:
answer = send.Content(msg);
break;
case 26:
answer = send.Insert(insertSong);
break;
default:
answer = false;
break;
}
#endregion
}
mySocket.Close();
}
}
/// <summary>
/// 将字符串解析转换为名值集合.
/// </summary>
/// <param name="queryString">字符串</param>
/// <returns></returns>
NameValueCollection GetQueryString(string queryString)
{
NameValueCollection result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
if (!string.IsNullOrEmpty(queryString))
{
int count = queryString.Length;
for (int i = 0; i < count; i++)
{
int startIndex = i;
int index = -1;
while (i < count)
{
char item = queryString[i];
if (item == '=')
{
if (index < 0)
{
index = i;
}
}
else if (item == '&')
{
break;
}
i++;
}
string key = null;
string value = null;
if (index >= 0)
{
key = queryString.Substring(startIndex, index - startIndex);
value = queryString.Substring(index + 1, (i - index) - 1);
}
else
{
key = queryString.Substring(startIndex, i - startIndex);
}
result[key] = value;
if ((i == (count - 1)) && (queryString[i] == '&'))
{
result[key] = string.Empty;
}
}
}
return result;
}
}
}