Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 08:55

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



C# HTTPWebRequest Freezes the program !

Discussion on C# HTTPWebRequest Freezes the program ! within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
C# HTTPWebRequest Freezes the program !

I created a small GET request using HTTPWEBREQUEST

Code:
            string URL = "http://google.com";
            HttpWebRequest GetRequest = (HttpWebRequest)WebRequest.Create(URL);
            GetRequest.Method = "GET";
            GetRequest.CookieContainer = Jar;
            GetRequest.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1";
            GetRequest.KeepAlive = true;
           
            HttpWebResponse GetResponse = (HttpWebResponse)GetRequest.GetResponse();
            StreamReader GetReader = new StreamReader(GetResponse.GetResponseStream());
            richTextBox1.Text = GetReader.ReadToEnd();
But the program totally freezes while getting the response

so i tried to use asynchronous but i cant understand it so anyone here can tell me how could i convert the code above to async so it wont freeze the program
badguy4you is offline  
Old 07/11/2012, 02:43   #2
 
elite*gold: 40
Join Date: Apr 2012
Posts: 673
Received Thanks: 83
You need to do the HttpWebRequest in another thread. For that you can use a Backgroundworker.
tobindev is offline  
Old 07/11/2012, 02:47   #3
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
Will try and tell ya

I get this error !

Cross-thread operation not valid: Control 'select' accessed from a thread other than the thread it was created on.

i think cuz only the UI thread can access UI components

any workaround this
badguy4you is offline  
Old 07/11/2012, 02:57   #4
 
elite*gold: 40
Join Date: Apr 2012
Posts: 673
Received Thanks: 83
This happens when you perform work on the GUI in the Backgroundworker_DoWork part. You can finally report sth to the GUI in the Completed Event without problems but if you want to tell sth to GUI while working you need the ReportProgress Method.
tobindev is offline  
Old 07/11/2012, 03:01   #5
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
i want to update the gui from the background worker cuz my code just adds and reads text from a richtextbox so must be updated

and the website name is captured from a textbox
badguy4you is offline  
Old 07/11/2012, 03:07   #6
 
elite*gold: 42
Join Date: Jun 2008
Posts: 5,425
Received Thanks: 1,888
Thats basic c# knowledge, I allready told you to learn the language.
MoepMeep is offline  
Old 07/11/2012, 03:08   #7
 
elite*gold: 40
Join Date: Apr 2012
Posts: 673
Received Thanks: 83
For this u need like I said the ReportProgress Method and the ProgressChanged Event.
tobindev is offline  
Old 07/11/2012, 03:09   #8
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
MoepMeep -> And i already told you dont replay to my thread if you will not say something useful (i am only asking in this cuz i never used the worker in any program) so IS THERE A HELL PROPLEM THAT I ASK IN SOMETHING HERE ? ofc no problem This is a community MEANS share what you got ( you may request HELP or give HELP )

When i can help someone i just help not just saying go learn to free myself
badguy4you is offline  
Old 07/11/2012, 14:25   #9
 
elite*gold: 0
Join Date: Nov 2007
Posts: 62
Received Thanks: 17
You can do it like this (not tested):

1. Add new extension for control invoking from non-gui threads.
Code:
internal static class CrossInvoker
    {
        internal static void InvokeEx<T>(this T control, Action<T> action)
            where T : ISynchronizeInvoke
        {
            if (control.InvokeRequired)
                control.Invoke(action, new object[] { control });
            else
                action(control);
        }
    }
So now all your controls have InvokeEx function.

2. Then edit your WebRequest code (something like this):
Code:
internal void MakeRequest()
        {
            const string url = "http://www.google.com/";

            var request = (HttpWebRequest) WebRequest.Create(url);
            request.Method = "GET";
            request.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1";
            request.KeepAlive = true;

            var response = (HttpWebResponse) request.GetResponse();
            var reader = new StreamReader(response.GetResponseStream());
            
            // Here we set 'Text' property of txtInput.
            txtInput.InvokeEx(c => c.Text = reader.ReadToEnd());
        }
3. Then run new thread somewhere in your project: (and maybe wait till it's done)
Code:
new Thread(MakeRequest) { IsBackground = true }.Start();
Demon-777 is offline  
Thanks
1 User
Old 07/12/2012, 08:16   #10
 
elite*gold: 7
Join Date: Jun 2012
Posts: 168
Received Thanks: 95
Nice thanks you
ЙôČhěάŧĪмPĞM-hacked is offline  
Reply


Similar Threads Similar Threads
[VB.NET] HttpWebRequest - Tutorial
01/06/2020 - Coding Tutorials - 101 Replies
Hey, da ich nun vermehrt PN's erhalten habe, in denen gefragt wird, wie man HttpWebRequests benutzt, erstelle ich hier ein kleines Tutorial und werde euch zeigen, wie ihr euch damit auf Epvp einloggen könnt. Habt ihr erstmal das Prinzip verstanden, könnt ihr es eigentlich überall anwenden und somit auch einen YouTube Account Creator / Bot und sonstige Sachen erstellen. Das einzige was wir eigentlich noch brauchen ist das Programm Live HTTP Headers (Firefox-Addon). So, nun startet ihr...
HttpWebRequest C# Browsergame Bot
12/07/2017 - .NET Languages - 4 Replies
Hi, i have the following problem and it would be great if s.o. could help me. I try to write a simple bot for a browsergame which uses the HttpWebRequest and Response classes from C#. Currently i try to log in with the following script but i only get back to the login page. When i try to catch a site for logged in members only i also only get the login page. There has to be a problem with the cookie but i don't know where :( class HttpHelper { private CookieContainer...
c# httpwebrequest Travian
12/07/2017 - .NET Languages - 2 Replies
Gude Leute ich hätt da mal ein anliegen ich will mir ein kleines Tool zur vereinfachten bedinung von Travian schreiben nur leider bekomme ich das irgendwie nicht auf die Reihe. da mein Code nicht so recht funktioniert hat habe ich mir den code von jemand anderes mal geklaut aber der ergibt das gleiche problem. Das Probleme ist er zeigt die Seite nicht richtig an und er baut keine truppen wenn ich auf Button1 click. wenn mir einer das vielleicht mal flicken könnte oder mir erklären...
[VB] HttpWebRequest Problem -.-
02/02/2012 - .NET Languages - 1 Replies
Hallo liebe Elitepvpers User, ich habe leider folgendes Problem: Ich möchte von dem Spam Email Anbieter "yopmail.com" meine Emails auslesen. Dazu muss ich mich auf der Webseite einfach mit "test" Anmelden Hierzu der HTTP Header : Siehe Anhang accname = Account Name im Beispiel "test"
[C#]MD5-Cheksumme mit HttpWebRequest oä
08/18/2010 - .NET Languages - 1 Replies
Ich will mir mal einen gescheiten Patcher für Metin2 machen.. Jetzt soll er von den Files auf meinem Server die Checksummen ausspucken. Die Datei sollte nicht heruntergeladen werden... System.IO.FileStream FileCheck = System.IO.File.OpenRead(Dateipfad); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvi der(); byte md5Hash = md5.ComputeHash(FileCheck);



All times are GMT +1. The time now is 08:55.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.