回顾WebRequest.create

时间:2017-06-13 09:36:31

标签: c# httpwebrequest

我每100毫秒调用一次代码。

private void First_Request()
{
    Console.WriteLine("Connection Request");

    var httpWebRequest = (HttpWebRequest)WebRequest.Create(LOGIN_SERVER); 

    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";
    httpWebRequest.KeepAlive = true;
    MAC = GetMacAddress().ToString();  

    string json = new JavaScriptSerializer().Serialize(new
    {
        id = ID,
        macaddress = MAC
    });


    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
        var result_header = httpResponse.Headers;

        string cookie = httpResponse.GetResponseHeader("Set-Cookie");

        if (result.Contains("true"))
        {
            ConnectionIs = true;
        }
        else if (result.Contains("false"))
        {
            ConnectionIs = false;
        }
    }
}

我想知道连接是否会断开,然后每当行

时建立新连接
var httpWebRequest = (HttpWebRequest)WebRequest.Create(LOGIN_SERVER); 
每100ms调用

以向服务器发送ID和MAC地址。

如果连接丢失,我想如何保持连接?

或者有可能的方法而不是使用WebRequest.Create吗?

0 个答案:

没有答案