http 发送指令

baidu_16849617 2014-06-24 05:37:40

<html>
<body>

<!-- Send text to websocket -->
usrname:<input id="userName" type="textbox" value="lipeiji" style="background:yellow"/><br/>
message:<input id="userInput" type="textbox" value="test" style="background:Aqua"/><br/>
_notice:<input id="userNotice" type="textbox" value="lole" style="background:yellow"/><br/>
<button onclick="sendMessage('{act:LOGIN,usr:'+document.getElementById('userName').value+',msg:90283075428eb555f5d3959cddba3f85}')">登陆</button>
<button onclick="sendMessage('{act:ASK,usr:'+document.getElementById('userName').value+',msg:'+document.getElementById('userInput').value+'}')">请求</button>
<button onclick="sendMessage('{act:BROADCAST,usr:'+document.getElementById('userName').value+',msg:'+document.getElementById('userInput').value+'}')">广播</button>
<button onclick="sendMessage('{act:NOTICE,usr:'+document.getElementById('userNotice').value+',msg:'+document.getElementById('userInput').value+'}')">通知</button>

<!-- Results -->
<hr/>
输入:
<div id="inmessage"></div>
<hr/>
输出:
<div id="outmessage"></div>

<script>
function sendMessage(text) {
document.getElementById('inmessage').innerHTML = text;
ws.send(text);
}

function showMessage(text) {
document.getElementById('outmessage').innerHTML = text;
}

var ws = new WebSocket('ws://58.40.18.94:9200/do.wsk');

showMessage('Connecting...');
ws.onopen = function() { showMessage('Connected!'); };
ws.onclose = function() { showMessage('DisConnected!'); };
ws.onmessage = function(msg) { showMessage(msg.data); };
ws.onerror = function(msg) { showMessage(msg.data); };
</script>
</body>
</html>


这个是HTML的源码,可以直接运行点击登录返回!OK说明登录成功。 但是怎么该成WinFrom的。
链接地址:ws://58.40.18.94:9200/do.wsk
请求格式:{act:LOGIN,usr:'lipeiji',msg:"90283075428eb555f5d3959cddba3f85"}
响应:{"act":"LOGIN","usr":"lipeiji","msg":"!ok"}
msg为!ok表示成功,其他表示登陆失败。

求高手帮忙
...全文
218 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
save4me 2014-06-26
  • 打赏
  • 举报
回复
看一下我上面给你的两个链接,如果你用第二个开源项目的类的话,首页上面有个简单的使用示例。因为你发送和接收的是json,所以可以使用JsonWebSocket。下面的代码可以试一下,没有测试过,具体的可以下载源码看一下里面的示例。

using WebSocket4Net;

WebSocket websocket = new JsonWebSocket("ws://58.40.18.94:9200/do.wsk");
websocket.Opened += new EventHandler(websocket_Opened);
websocket.Closed += new EventHandler(websocket_Closed);
websocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
websocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error);
websocket.Open();

private void websocket_Opened(object sender, EventArgs e)
{
	showMessage("Connected!");
}
private void websocket_Closed(object sender, EventArgs e)
{
	showMessage("DisConnected!");
}
private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
{
	showMessage(e.Message);
}
private void websocket_Error(object sender, ErrorEventArgs e)
{
	showMessage(e.Message);
}

private void sendMessage(string text)
{
	MessageBox.Show(text);
    //websocket.Send(text, null);
    websocket.Send("{act:LOGIN,usr:'lipeiji',msg:'90283075428eb555f5d3959cddba3f85'}", null);
}
     
private void showMessage(string text) {
	MessageBox.Show(text);
}
引用 6 楼 save4me 的回复:
HttpWebRequest是用来实现http协议的,你的是ws协议,比如http://ws://58.40.18.94:9200/do.wsk这个都根本不是合法的http地址,当然连接不上了。
引用 4 楼 baidu_16849617 的回复:
是这样的吧? 但是一直报错说连接不是远程服务器。
baidu_16849617 2014-06-26
  • 打赏
  • 举报
回复
引用 2 楼 save4me 的回复:
使用WebSocket编程吧?可以参考一下: .NET平台下websocket协议的实现! 或者开源项目 WebSocket4Net
我要把这个WebSocket编程的 改成WinFrom的。
baidu_16849617 2014-06-26
  • 打赏
  • 举报
回复
引用 3 楼 walkeeper 的回复:
用HttpWebRequest 朝这个地址发送请求 用HttpWebResponse获取响应的内容再解析JSON串来判断吧
#region httpwraper public static ErrorType HttpRequest(out HttpResp resp) { resp = null; string str = "{act:LOGIN,usr:'lipeiji',msg:"90283075428eb555f5d3959cddba3f85"}"; //log.Info(str); byte[] data = Encoding.UTF8.GetBytes(str); HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ws://58.40.18.94:9200/do.wsk"); request.Timeout = 30000; request.Credentials = CredentialCache.DefaultCredentials; request.Proxy = GlobalProxySelection.GetEmptyWebProxy(); request.Method = "POST"; request.ContentLength = data.Length; request.ContentType = "text/xml; charset=\"utf-8\""; //ServicePointManager.Expect100Continue = false; try { using (Stream streamBody = request.GetRequestStream()) { streamBody.Write(data, 0, data.Length); streamBody.Flush(); streamBody.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream dataStream = response.GetResponseStream()) { if (dataStream == null) { return ErrorType.Failed; } TextReader reader = new StreamReader(dataStream, Encoding.UTF8); string responseStr = reader.ReadToEnd(); HttpResp obj = XmlWrapper.Xml2Object(responseStr, typeof(HttpResp)) as HttpResp; resp = obj; if (obj == null) { log.ErrorFormat("UploadFailed, request = {0}, response = {1}", str, responseStr); return ErrorType.Failed; } if (resp.st != "ok") { log.ErrorFormat("UploadFailed, request = {0}, response = {1}", str, responseStr); return ErrorType.Failed; } TrySyncTime1(resp); return ErrorType.Success; } } catch (ThreadAbortException) { return ErrorType.Failed; } catch (ObjectDisposedException) { return ErrorType.Failed; } catch (SocketException ex) { return ErrorType.Failed; } catch (WebException ex) { request.Abort(); log.Debug(ex.Message); //WebResponse wr = ex.Response; //using (Stream stream = wr.GetResponseStream()) //{ // if (stream != null) // { // StreamReader streamReader = new StreamReader(stream, Encoding.Default); // string strError = streamReader.ReadToEnd(); // streamReader.Close(); // } //} return ErrorType.NetworkError; } catch (Exception ex) { log.Error("错误:" + ex.ToString()); return ErrorType.Failed; } } private static void TrySyncTime1(HttpResp resp) { try { if (resp == null || resp.Header == null) { return; } if (string.IsNullOrEmpty(resp.Header.time)) { return; } DateTime serverTime = Convert.ToDateTime(resp.Header.time); if (Math.Abs((serverTime - DateTime.Now).TotalSeconds) >= 180) { DateTimeHelper.LocalTime = serverTime; } } catch (Exception) { } } #endregion 是这样的吧? 但是一直报错说连接不是远程服务器。
save4me 2014-06-26
  • 打赏
  • 举报
回复
HttpWebRequest是用来实现http协议的,你的是ws协议,比如http://ws://58.40.18.94:9200/do.wsk这个都根本不是合法的http地址,当然连接不上了。
引用 4 楼 baidu_16849617 的回复:
是这样的吧? 但是一直报错说连接不是远程服务器。
walkeeper 2014-06-25
  • 打赏
  • 举报
回复
用HttpWebRequest 朝这个地址发送请求 用HttpWebResponse获取响应的内容再解析JSON串来判断吧
save4me 2014-06-25
  • 打赏
  • 举报
回复
使用WebSocket编程吧?可以参考一下: .NET平台下websocket协议的实现! 或者开源项目 WebSocket4Net
wmg494005678 2014-06-24
  • 打赏
  • 举报
回复
HttpWebRequest
(1).测试案例(Testcase)通过Client Lib的接口向Selenium Server发送Http请求,要求和Selenium Server建立连接。 为什么要通过发送Http请求控制Selenium Server而不采用其他方式呢?从上文可以看出,Selenium Server是一个独立的中间服务器(确切地说是代理服务器),它可以架设在其他机器上!所以测试案例通过发送HTTP请求去控制Selenium Server是很正常的。 (2).Selenium Server的Launcher启动浏览器,把Selenium Core加载入浏览器页面当中,并把浏览器的代理设置为Selenium Server的Http Proxy。 (3).测试案例通过Client Lib的接口向Selenium Server发送Http请求,Selenium Server对请求进行解析,然后通过Http Proxy发送JS命令通知Selenium Core执行操作浏览器的动作。 (4).Selenium Core接收到指令后,执行操作。 (5).浏览器收到新的页面请求信息(因为在(4)中,Selenium Core的操作可能引发新的页面请求),于是发送Http请求,请求新的Web页面。 由于Selenium Server在启动浏览器时做了手脚,所以Selenium Server会接收到所有由它启动的浏览器发送的请求。 (6).Selenium Server接收到浏览器的发送Http请求后,自己重组Http请求,获取对应的Web页面。 (7).Selenium Server的Http Proxy把接收的Web页面返回给浏览器。 因为浏览器存在同源策略,所以Selenium RC中的Selenium Server需要以这种代理模式运行。

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧