c# – What Threads Share When Making Http Calls?


The problem: I can't log in to a site more than once on different Threads .

My application is a Console application .
If I open several executables , each one can successfully log into a Thread , only the first one . I believe this rules out several issues that I could have related to the site I'm accessing, as multiple open Consoles use the same credentials (user), are running on the same machine, and open at the same time. Therefore, the site allows more than one session for the same user.

I'm using the same solution for multiple sites and it worked for most I needed to do this. The sites are unrelated to each other and I don't care what technology they use (I don't think it's relevant, but I can check if necessary).

Threads are started with new Thread(IniciaTarefa).Start(argumentos); . The IniciaTarefa method is static, but inside it instantiates another class that has nothing static. And it is this other class that makes the requests.

Each request is instantiated a new request = WebRequest.Create(url) as HttpWebRequest; , although all requests from the same Thread share the same CookieContainer to maintain the session.

I'll take any hints I can investigate because I have no idea what the problem might be, I've already reviewed and tried to make several changes to the code, but the problem persists.
If necessary, I can put specific snippets of code, but I don't know what might be relevant.


The problem is the CookieContainer being shared by the Threads… When you log in, the website you access saves a cookie in your browser (in this case, the cookie is stored in the CookieContainer) and it is through it that the website gets identify that you are you, from what you said, the CookieContainer is being reused by the various Threads of the same process, causing the login to occur in the first and second onwards the site already identifies that you are logged in exactly for the reason of reusing the cookieContainer. Try creating a CookieContainer on request, just like you do with WebRequest.Create

Scroll to Top