简体   繁体   中英

webserver forcing browser to only open one parallel connection

Hello I'm trying to write a webserver in C#.
The server is going to dynamically create a website based on some templates I defined.
The problem I have is that you only can access the webpage if you enter a password.
So I decided to make the browser open up a keep-alive connection, passing every request through it.
Then I have control over logged in clients and not logged in clients. Now the problem is that Firefox and Google Chrome, when it comes to requesting the images on the website, they just open up another connection from the same ip but a different port.
My webserver thinks that its another client and sends the login http page instead of the requested image.
So every time the website loads only 1 - 4 images are getting actually sent.

Now my question: Is there any way to force the browser NOT to open up parallel connections?
Or if not possible how should I deal with the problem?

For those who like to see some code here is what the core of the server looks like, just to understand my problem:

void ThreadStart()
{
    while (true)
    {
        RunClient(listener.AcceptTcpClient());
    }
}
void RunClient(TcpClient c)
{
        Thread tht = new Thread(new ParameterizedThreadStart(RunIt));
        tht.IsBackground = true;
        tht.Start(c);//The login page is getting sent...
        thtt.Add(tht);
}

Thanks in advance, Alex

Authenticating a HTTP connection rather than individual requests is wrong, wrong, wrong. Even if you could make the browser reuse a single connection (which you can't, because that's not how HTTP works), you wouldn't be able to count on this being respected by proxies or transparent web caches.

This is (some of) what cookies were invented for. Use them, or some kind of session identifier built into the URLs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM