How to get server transfer data in C #?

I am writing a program. my program receives data from the server via the HTTP protocol. the data will be redirected by the server to my program. I tried using WebRequest but got only one data session. How can I maintain a network connection, constantly receive data from the server, Any help is appreciated.

Below is the SDK document:

With the permission of GUEST or ADMIN, you can get a series of live images (Server load). To get the images, send a request to "/liveimg.cgi?serverpush=1" as shown. 2-1-1.
 When the camera receives the above request from the client, it sends a refund, as shown in the figure. 2-2.
  Each JPEG data is separated by "-myboundary", and "image / jpeg" is returned as a "Content-Type" header, after "--myboundary". For the Content-Length header, it returns the number of bytes in the -myboundary data (excluding "-myboundary", each header, and \ r \ n as a delimiter). After the header "Content-Length" and "\ r \ n" (delimiter) the actual data will be sent.
  This data transfer will continue until the client stops the connection (disconnect) or some network error occurs.

int len; string uri = @ " http://192.168.0.2/liveimg.cgi?serverpush=1 ";

        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
        req.Credentials = new NetworkCredential("admin", "admin");
        req.KeepAlive = true;

        string line = "";

        HttpWebResponse reply = (HttpWebResponse)req.GetResponse();
        Stream stream = reply.GetResponseStream();

        System.Diagnostics.Debug.WriteLine(reply.ContentType);

        StreamReader reader = new StreamReader(stream);
        do
        {
            line = reader.ReadLine();
            System.Diagnostics.Debug.WriteLine(line);

            System.Threading.Thread.Sleep(300);

        } while (line.Length>0);
+3
source share
5 answers

You can keep the HTTP connection open for a long period of time if the server supports this. (As already mentioned, this will significantly limit the number of concurrent users that you can support.)

Response.Buffer = false ScriptTimeout ( , ASP.NET ). , Response.Write , , .

, .

+2

StreamHub Push Server - Comet .NET Client SDK, # ( VB/++).

+2

, - , , /. ? , , - , .

, . , , .

, , , - , -, - . , .. , WCF, , , .

+1

IP- cookie HttpWebRequest. "index.html".
...

BitmapObject - , Jpeg, . , 200 . , "serverpush".

public void Connect()
{
    try
    {
        request = (HttpWebRequest)WebRequest.Create("Http://192.168.0.2/index.html");

        request.Credentials = new NetworkCredential(UserName,Password);
        request.Method = "GET";
        response = (HttpWebResponse)request.GetResponse();
        WebHeaderCollection headers = response.Headers;
        Cookie = headers["Set-Cookie"];//get cookie

        GetImage(null);
    }
    catch (Exception ex)
    {
        BitmapObject bitmap = new BitmapObject(Properties.Resources.Off,DateTime.Now);
        bitmap.Error = ex.Message;
        onImageReady(bitmap);
    }
}

private Stream GetStream()
{
    Stream s = null;
    try
    {
        request = (HttpWebRequest)WebRequest.Create("http://192.168.0.2/liveimg.cgi");
        if (!Anonimous)
            request.Credentials = new NetworkCredential(UserName, Password);
        request.Method = "GET";
        request.KeepAlive = KeepAlive;

        request.Headers.Add(HttpRequestHeader.Cookie, Cookie);
        response = (HttpWebResponse)request.GetResponse();
        s = response.GetResponseStream();

    }
    catch (Exception ex)
    {
        BitmapObject bitmap = new BitmapObject(Properties.Resources.Off,DateTime.Now);
        bitmap.Error = ex.Message;
        onImageReady(bitmap);
    }
    return s;
}

public void GetImage(Object o)
{
    BitmapObject bitmap = null;
    stream = GetStream();
    DateTime CurrTime = DateTime.Now;
    try
    {
        bitmap = new BitmapObject(new Bitmap(stream),CurrTime);
        if (timer == null)//System.Threading.Timer
            timer = new Timer(new TimerCallback(GetImage), null, 200, 200);
    }
    catch (Exception ex)
    {
        bitmap = new BitmapObject(Properties.Resources.Off, CurrTime);
        bitmap.Error = ex.Message;
    }
    finally
    {
        stream.Flush();
        stream.Close();
    }
    onImageReady(bitmap);
}
+1

-, - .

To really get the data on the server, you must create such a server yourself.

0
source

Source: https://habr.com/ru/post/1710602/


All Articles