因此,我正在尝试为特定服务器编写客户端,模仿我在Chrome和Firefox等浏览器的开发工具中看到的行为。我正在使用httpwebrequest,这似乎对我来说很适合个人交流,但是一旦我需要使用另一个交换,它就永远无法正常工作。我认为我只需要存储我的ConnectionToken,以便服务器可以在多个请求中识别我为同一客户端,但显然不是这样。这是一个例子。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Web.Script.Serialization;
namespace WebClient
{
class WebClientProgram
{
static void Main(string[] args)
{
//FIRST REQUEST, NEGOTIATE
DateTime unixStart = DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc);
long epoch = (long)Math.Floor((DateTime.Now.ToUniversalTime() - unixStart).TotalSeconds);
String timestamp = Convert.ToString(epoch);
String uri = "http://domain/signalr/negotiate?_=" + timestamp;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
//Set REQUEST values
request.Credentials = new NetworkCredential("username", "password");
request.Method = "GET";
request.KeepAlive = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
JavaScriptSerializer jSerialize = new JavaScriptSerializer();
Dictionary<string, string> responseDictionary = jSerialize.Deserialize<Dictionary<string, string>>(reader.ReadToEnd());
reader.Close();
response.Close();
String connectionToken = responseDictionary["ConnectionToken"];//Save my unique connection token
String connectionID = responseDictionary["ConnectionId"];
String protocolVersion = responseDictionary["ProtocolVersion"];
//SECOND REQUEST, START
epoch = (long)Math.Floor((DateTime.Now.ToUniversalTime() - unixStart).TotalSeconds);
timestamp = Convert.ToString(epoch);
uri = "http://domain/signalr/start?clientProtocol=" + protocolVersion + "&connectionToken=" + connectionToken + "&_=" + timestamp;
request = (HttpWebRequest)WebRequest.Create(uri);
//Set REQUEST values
request.Credentials = new NetworkCredential("username", "password");
request.Method = "GET";
request.KeepAlive = true;
response = (HttpWebResponse)request.GetResponse();//On this line I receive a 400 error from the server
reader = new StreamReader(response.GetResponseStream());
File.WriteAllText(@"start.txt", reader.ReadToEnd());
}
}
}
所以这是我试图对SignalR服务器进行基本交换,这就是我试图附加的。我首先进行谈判,并恢复以下请求的独特连接风格。但是,以下请求始终遇到响应" 400:连接-ID为不正确的格式"。有什么想法吗?谢谢大家。
仅查看您的代码,您是否缺少URL中的and和?
"&connectionToken=" + connectionToken + "
在这里 _=" + timestamp