HttpWebRequest simultaneously!!!!
-
HI... I managed searching in google to make a call to a server which returns me data from different GPS devices so I can save to a database. The problem is that I need to do the same but in two different server at the same time, ie, I need to get data from two different URL and save them in my database. This is what I achieved. try { byte[] buf = new byte[8192]; HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://xxx.xxx.xxx.xxx:7778/online?user=xx&pass=xxx"); HttpWebResponse response = (HttpWebResponse) request.GetResponse(); Stream resStream = response.GetResponseStream(); //int count = 0; //string tempString = ""; Thread t = new Thread(delegate() { Go(buf, resStream, listaRelojes); }); // No need to explicitly use ThreadStart t.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message); } public void Go(byte[] buf, Stream resStream, LinkedList<reloj> listaRelojes) { int count = 0; string tempString = ""; tempString = Encoding.ASCII.GetString(buf, 0, count); do { count = resStream.Read(buf, 0, buf.Length); if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); Parse(tempString, listaRelojes); System.Console.Out.WriteLine("my System: " + tempString); } } while (count > 0); } I need something like this I think : WebRequest.Create("http://xxx.xxx.xxx.xxx:7778/online?user=xx&pass=xxx"); and WebRequest.Create("http://yyy.yyy.yyy.yyy:7779/online?user=yy&pass=yyy"); at the same time. Is this posible??? Thank´s in advance. Christian...
-
HI... I managed searching in google to make a call to a server which returns me data from different GPS devices so I can save to a database. The problem is that I need to do the same but in two different server at the same time, ie, I need to get data from two different URL and save them in my database. This is what I achieved. try { byte[] buf = new byte[8192]; HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://xxx.xxx.xxx.xxx:7778/online?user=xx&pass=xxx"); HttpWebResponse response = (HttpWebResponse) request.GetResponse(); Stream resStream = response.GetResponseStream(); //int count = 0; //string tempString = ""; Thread t = new Thread(delegate() { Go(buf, resStream, listaRelojes); }); // No need to explicitly use ThreadStart t.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message); } public void Go(byte[] buf, Stream resStream, LinkedList<reloj> listaRelojes) { int count = 0; string tempString = ""; tempString = Encoding.ASCII.GetString(buf, 0, count); do { count = resStream.Read(buf, 0, buf.Length); if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); Parse(tempString, listaRelojes); System.Console.Out.WriteLine("my System: " + tempString); } } while (count > 0); } I need something like this I think : WebRequest.Create("http://xxx.xxx.xxx.xxx:7778/online?user=xx&pass=xxx"); and WebRequest.Create("http://yyy.yyy.yyy.yyy:7779/online?user=yy&pass=yyy"); at the same time. Is this posible??? Thank´s in advance. Christian...
If you can open up two different browser instances, and successfully issue those URL requests in each of them simultaneously, I see no reason why two WebRequests wouldn't work equally well. However, I never did anything of that kind. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
HI... I managed searching in google to make a call to a server which returns me data from different GPS devices so I can save to a database. The problem is that I need to do the same but in two different server at the same time, ie, I need to get data from two different URL and save them in my database. This is what I achieved. try { byte[] buf = new byte[8192]; HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://xxx.xxx.xxx.xxx:7778/online?user=xx&pass=xxx"); HttpWebResponse response = (HttpWebResponse) request.GetResponse(); Stream resStream = response.GetResponseStream(); //int count = 0; //string tempString = ""; Thread t = new Thread(delegate() { Go(buf, resStream, listaRelojes); }); // No need to explicitly use ThreadStart t.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message); } public void Go(byte[] buf, Stream resStream, LinkedList<reloj> listaRelojes) { int count = 0; string tempString = ""; tempString = Encoding.ASCII.GetString(buf, 0, count); do { count = resStream.Read(buf, 0, buf.Length); if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); Parse(tempString, listaRelojes); System.Console.Out.WriteLine("my System: " + tempString); } } while (count > 0); } I need something like this I think : WebRequest.Create("http://xxx.xxx.xxx.xxx:7778/online?user=xx&pass=xxx"); and WebRequest.Create("http://yyy.yyy.yyy.yyy:7779/online?user=yy&pass=yyy"); at the same time. Is this posible??? Thank´s in advance. Christian...
Try threading with BackgroundWorker, Threads or CCR. I have done it myself on a recent project using CCR. But at first instance i suggest you give the BackgroundWorkers a shot. Regards Jens
When in trouble, when in doubt, run in circles, scream and shout
-
HI... I managed searching in google to make a call to a server which returns me data from different GPS devices so I can save to a database. The problem is that I need to do the same but in two different server at the same time, ie, I need to get data from two different URL and save them in my database. This is what I achieved. try { byte[] buf = new byte[8192]; HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://xxx.xxx.xxx.xxx:7778/online?user=xx&pass=xxx"); HttpWebResponse response = (HttpWebResponse) request.GetResponse(); Stream resStream = response.GetResponseStream(); //int count = 0; //string tempString = ""; Thread t = new Thread(delegate() { Go(buf, resStream, listaRelojes); }); // No need to explicitly use ThreadStart t.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message); } public void Go(byte[] buf, Stream resStream, LinkedList<reloj> listaRelojes) { int count = 0; string tempString = ""; tempString = Encoding.ASCII.GetString(buf, 0, count); do { count = resStream.Read(buf, 0, buf.Length); if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); Parse(tempString, listaRelojes); System.Console.Out.WriteLine("my System: " + tempString); } } while (count > 0); } I need something like this I think : WebRequest.Create("http://xxx.xxx.xxx.xxx:7778/online?user=xx&pass=xxx"); and WebRequest.Create("http://yyy.yyy.yyy.yyy:7779/online?user=yy&pass=yyy"); at the same time. Is this posible??? Thank´s in advance. Christian...
You can create the two requests as you like and then call BeginGetResponse() on each of them (MSDN Reference[^]). This will probably be the easiest way to get what you want without getting entangled with thread pools (and possible starvation).