Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 06-25-07   #1 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,112

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 234
Trader Rating: 1
Default C# HttpwebRequest cookies n' stuff

Allright, so I'm writing an application to simulate the actions of a user in a web based program we've written. I want to write it for load testing, meaning I'll be hitting the server with thousands of requests at once.

But before I get trigger happy, I need to get my httpwebrequests working properly. The basic idea is I read some XML out of a file that is exactly what would be sent to the server if a user attempted to login though the web. (this was captured through fiddler). I want the server to think I am attempting to log in, with the proper username/password, and send me the proper xml response in return. I had written it properly as described, but I kept getting a "Session Timed Out or Not Available" XML response from the server. So now I'm thinking it needs me to initialize the session by sending a cookie (that is usually recieved initially when loading the login page) with the request to login. My problem is that I am not able to send a blank request(to simulate loading the login page) and apply the cookie recieved in the second web request (to simulate the actual logging in). In fact, I can't seem to get two consecutive requests to run at all. I think if I figure that out I'll be in better shape. I am able to comment out the initial web request/response and just send the login request and it works fine, except for the "Session Timed Out or Not Available". When I try to do the initial web request/response and then do the login, I get an error thrown saying "System.IO.IOException: Unable to write data to the transport connection: An established connection was aborted by the software in your host machine."

Here's code:

Code:
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Web;
using System.Security.Cryptography.X509Certificates;

namespace ping
{
    class SimpleHttpClient
    {
        public static byte[] StrToByteArray(string str)
        {
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            return encoding.GetBytes(str);
        }

        [STAThread]
        public void login()
        {
            //string gatewayUrl = "http://p8dev01web:7001/core/pswsapi/pswsapi.asp";
            string inputXmlQueryFilePath = "C:/Documents and Settings/krainbolt/My Documents/Visual Studio 2005/Projects/serverTestApp/serverTestApp/logon.txt";
            StreamWriter output = new StreamWriter(@"C:/Documents and Settings/krainbolt/My Documents/Visual Studio 2005/Projects/serverTestApp/serverTestApp\\Output.txt", false);
            StreamWriter soutput = new StreamWriter(@"C:/Documents and Settings/krainbolt/My Documents/Visual Studio 2005/Projects/serverTestApp/serverTestApp\\startOut.txt", false);
            string cookieHeader = null;


            try
            {
                

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myserver:80/core/");
                request.Method = "Post";
                CookieContainer cookieJar = new CookieContainer();
                request.CookieContainer = cookieJar;
                byte[] requestBytes = StrToByteArray(" ");
                request.ContentLength = requestBytes.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(requestBytes, 0, requestBytes.Length);
                requestStream.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseData = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseData);
                string responseFromServer = reader.ReadToEnd();
                
                cookieHeader = response.Cookies.ToString();
                soutput.Write(cookieHeader);
                soutput.Write(responseFromServer);
                soutput.Write(response.Headers[4]);
                responseData.Close();
                soutput.Close();
                reader.Close();
                response.Close();

                
                
                HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://myserver:80/login.asp");
                // read input XML query from file
                string inputXml;
                using (StreamReader inputQueryReader = new StreamReader(inputXmlQueryFilePath))
                {
                    inputXml = inputQueryReader.ReadToEnd();
                }
             
                byte[] request1Bytes = StrToByteArray(inputXml);
                request1.Method = "Post";
                //request1.CookieContainer.Add(response.Cookies);
                request1.ContentLength = request1Bytes.Length;
                Stream request1Stream = request1.GetRequestStream();
                //output.Write(request1.CookieContainer.ToString());
                request1Stream.Write(request1Bytes, 0, request1Bytes.Length);
                request1Stream.Close();

                HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
                Stream response1Data = response1.GetResponseStream();
                StreamReader reader1 = new StreamReader(response1Data);
                string response1FromServer = reader1.ReadToEnd();

                
                output.Write(response1FromServer);
                response1.Close();
                reader1.Close();
                output.Close();

                //
                
            }
            catch (Exception e)
            {
                output.Write("Exception: " + e);
                output.Close();
            }
            
        }

    }
}
I'm thinking there must be a way to keep the connection alive and use the same httpwebrequest object for both calls, rather then use two separate ones. Right now I'm not even trying to deal with the cookies, I just want to get the sequential server requests working. Then I'll deal with either using two objects, or modifying one to send a second request to the server? any suggestions?
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 06-25-07   #2 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,112

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 234
Trader Rating: 1
Default

heh, probably not to many .NET programmers on here, poo.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 06-25-07   #3 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,112

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 234
Trader Rating: 1
Default

Well I've stripped away all the fluff off of my problem, and what I'm really having problems with is how to send a request to the server, get a response, and then send the exact same request (or a different one) using the cookie received by the first response. I have successfully sent 2 sequential requests to the server, but each one has its own cookie. I want the second to send the first's cookie so the server knows it's the same session. Who's got a thought. All the programmers here are old school VB6 and such, not to much knowledge on .NET.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 06-25-07   #4 (permalink)
Bifford
 
BFRD's Avatar
 
intel nvidia

Join Date: Dec 2004
Location: Carrollton, TX
Posts: 4,336

FAQs Submitted: 8
Folding Team Rank: 27
Hardware Reviews: 2
Trader Rating: 11
Default

I don't think you will be able to reuse the same request. The nature of the request would limit its usability to a single operation.

It looks like a lot of your close methods were out of order. I don't know if that should have a great effect on the outcome, but it is worth a shot. Also any object that implements IDisposable can use the "using" syntax. That will make the structure much easier to read.

Code:
using System;                                                                                                                                                                         
using System.Net;                                                                                                                                                                     
using System.IO;                                                                                                                                                                      
using System.Text;                                                                                                                                                                    
using System.Web;                                                                                                                                                                     
using System.Security.Cryptography.X509Certificates;                                                                                                                                  
 
namespace ping                                                                                                                                                                        
{                                                                                                                                                                                     
  class SimpleHttpClient                                                                                                                                                            
  {                                                                                                                                                                                 
      public static byte[] StrToByteArray(string str)                                                                                                                               
      {                                                                                                                                                                             
          System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();                                                                                                     
          return encoding.GetBytes(str);                                                                                                                                            
      }                                                                                                                                                                             
 
      [STAThread]                                                                                                                                                                   
      public void login()                                                                                                                                                           
      {                                                                                                                                                                             
          //string gatewayUrl = "http://p8dev01web:7001/core/pswsapi/pswsapi.asp";                                                                                                  
          string inputXmlQueryFilePath = "C:/Documents and Settings/krainbolt/My Documents/Visual Studio 2005/Projects/serverTestApp/serverTestApp/logon.txt";                      
          StreamWriter output = new StreamWriter(@"C:/Documents and Settings/krainbolt/My Documents/Visual Studio 2005/Projects/serverTestApp/serverTestApp\\Output.txt", false);   
          StreamWriter soutput = new StreamWriter(@"C:/Documents and Settings/krainbolt/My Documents/Visual Studio 2005/Projects/serverTestApp/serverTestApp\\startOut.txt", false);
          string cookieHeader = null;                                                                                                                                               
 
 
          try                                                                                                                                                                       
          {                                                                                                                                                                         
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myserver:80/core/");                                                                               
              request.Method = "Post";                                                                                                                                              
              CookieContainer cookieJar = new CookieContainer();                                                                                                                    
              request.CookieContainer = cookieJar;                                                                                                                                  
              byte[] requestBytes = StrToByteArray(" ");                                                                                                                            
              request.ContentLength = requestBytes.Length;                                                                                                                          
 
              using (Stream requestStream = request.GetRequestStream())                                                                                                             
              {                                                                                                                                                                     
                  requestStream.Write(requestBytes, 0, requestBytes.Length);                                                                                                        
              }                                                                                                                                                                     
 
              using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())                                                                                             
              {                                                                                                                                                                     
                  using (Stream responseData = response.GetResponseStream())                                                                                                        
                  {                                                                                                                                                                 
                      using (StreamReader reader = new StreamReader(responseData))                                                                                                  
                      {                                                                                                                                                             
                          string responseFromServer = reader.ReadToEnd();                                                                                                           
 
                          cookieHeader = response.Cookies.ToString();                                                                                                               
                          soutput.Write(cookieHeader);                                                                                                                              
                          soutput.Write(responseFromServer);                                                                                                                        
                          soutput.Write(response.Headers[4]);                                                                                                                       
                          soutput.Close();                                                                                                                                          
                      }                                                                                                                                                             
                  }                                                                                                                                                                 
              }                                                                                                                                                                     
 
 
              HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://myserver:80/login.asp");                                                                          
              // read input XML query from file                                                                                                                                     
              string inputXml;                                                                                                                                                      
              using (StreamReader inputQueryReader = new StreamReader(inputXmlQueryFilePath))                                                                                       
              {                                                                                                                                                                     
                  inputXml = inputQueryReader.ReadToEnd();                                                                                                                          
              }                                                                                                                                                                     
 
              byte[] request1Bytes = StrToByteArray(inputXml);                                                                                                                      
              request1.Method = "Post";                                                                                                                                             
              //request1.CookieContainer.Add(response.Cookies);                                                                                                                     
              request1.ContentLength = request1Bytes.Length;                                                                                                                        
 
              using (Stream request1Stream = request1.GetRequestStream())                                                                                                           
              {                                                                                                                                                                     
                  //output.Write(request1.CookieContainer.ToString());                                                                                                              
                  request1Stream.Write(request1Bytes, 0, request1Bytes.Length);                                                                                                     
              }                                                                                                                                                                     
 
              using (HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse())                                                                                           
              {                                                                                                                                                                     
                  using (Stream response1Data = response1.GetResponseStream())                                                                                                      
                  {                                                                                                                                                                 
                      using (StreamReader reader1 = new StreamReader(response1Data))                                                                                                
                      {                                                                                                                                                             
                          string response1FromServer = reader1.ReadToEnd();                                                                                                         
                          output.Write(response1FromServer);                                                                                                                        
                      }                                                                                                                                                             
                  }                                                                                                                                                                 
              }                                                                                                                                                                     
 
          }                                                                                                                                                                         
          catch (Exception e)                                                                                                                                                       
          {                                                                                                                                                                         
              output.Write("Exception: " + e);                                                                                                                                      
              output.Close();                                                                                                                                                       
          }                                                                                                                                                                         
 
      }                                                                                                                                                                             
 
  }                                                                                                                                                                                 
}                                                                                                                                                                                     
EDIT: I am not sure that declaring the output writers publically like that is the best way to go. However, I can't spend a lot of time looking at it right now. Perhaps a little later I will have some more time.
__________________
Helpful Posts (Hopefully )
Overclocker's Calculator
Photo Editing - B&W w/Color Accents

System: Main Rig
CPU
E6700 Conroe
Motherboard
Abit QuadGT
Memory
2GB G.Skill PC2 8000 (HZ)
Graphics Card
EVGA 8800GTX
Hard Drive
150 GB Raptor X / 300GB Storage
Sound Card
Audigy 2 ZS
Power Supply
PCP&C Silencer 750
Case
Sigma Shark
CPU cooling
Stock (for now)
GPU cooling
Stock
OS
Vista Ultimate
Monitor
Dual Samsung 204b

Last edited by BFRD : 06-25-07 at 01:45 PM.
BFRD is offline I fold for Overclock.net Overclocked Account BFRD's Gallery   Reply With Quote
Old 06-25-07   #5 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,112

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 234
Trader Rating: 1
Default

BFRD, thanks so much for taking a look at it! I was actually able to get the same httpwebrequest to run more than once on the server. And using a cookiecontainer I was able to continue to send the same cookie the second time. Now I'm running into a wall where I'm trying to post to the server, and do it many times in a row, but after the first time I get a "Unable to write data to the transport connection: An established connection was aborted by the software in your host machine." I have no idea if it really is my machine, or it just doesn't like posting more then once. However, when I run it on the loop the entire post call goes out of scope and restarts, so it should have no memory of itself previously logging in, yet it still stops me the second time.
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 06-26-07   #6 (permalink)
4.0ghz
 
Knitelife's Avatar
 
intel ati

Join Date: Mar 2007
Location: Saint Louis, MO, USA
Posts: 827

Rep: 180 Knitelife is acknowledged by manyKnitelife is acknowledged by many
Unique Rep: 110
Trader Rating: 1
Default

kdbolt,

Can you post the code you used in your loop. I used to do this type of thing to scrape data from a clients website. I will dig up my old code and see how it compares to what you are doing.

System: Main Folding Rig
CPU
Q6600 @ 4.1Ghz
Motherboard
Asus P5K3-Deluxe
Memory
1GB Kingston DDR3-1375
Graphics Card
Rage 3D
Hard Drive
USB Jump Drive running Ubuntu
Power Supply
Silverstone Zues
Case
Lian-Li PC-60
CPU cooling
Vapochill Lightspeed
OS
Suse Enterprise Desktop 10.2
Monitor
KVM-Switch
Knitelife is offline Overclocked Account   Reply With Quote
Old 06-26-07   #7 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel ati

Join Date: May 2007
Location: Walled Lake, MI
Posts: 1,112

Rep: 127 kdbolt70 is acknowledged by manykdbolt70 is acknowledged by many
Unique Rep: 92
Folding Team Rank: 234
Trader Rating: 1
Default

Code:
for (int i = 0; i < x; i++)
                {
                    CookieContainer cookieJar = new CookieContainer();
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myServer");
                    request.Method = "Get";
                    request.CookieContainer = cookieJar;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream responseData = response.GetResponseStream();

                    response.Close();

                    //*---Send Login XML----*

                    request = (HttpWebRequest)WebRequest.Create("http://MyServer ");
                    request.Method = "Post";
                    request.CookieContainer = cookieJar;
                    request.KeepAlive = true;

                    string inputXml;
                    using (StreamReader inputQueryReader = new StreamReader(inputXmlQueryFilePath))
                    {
                        inputXml = inputQueryReader.ReadToEnd();
                    }

                    byte[] requestBytes = StrToByteArray(inputXml);
                    request.ContentLength = requestBytes.Length;
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(requestBytes, 0, requestBytes.Length);
                    requestStream.Flush();
                    requestStream.Close();

                    response = (HttpWebResponse)request.GetResponse();
                    responseData = response.GetResponseStream();
                    response.Close();
                    output.Close();
                    //reader.Close();
                    responseData.Close();
                }

It fails on the second execution of

requestStream.Write(requestBytes, 0, requestBytes.Length);

Whats interesting is that it will "Get" just fine, but it will only allow one "Post". When I try the second one it gives me a: "Unable to write data to the transport connection: An established connection was aborted by the software in your host machine."
__________________

~M Hail to the Victors M~

System: It's about time!
CPU
Q6600 G0 @3.3Ghz
Motherboard
Gigabyte P35-DS3L
Memory
2Gb Ballistix DDR2 800 @915Mhz
Graphics Card
Sapphire 2900Pro Flashed to XT
Hard Drive
Seagate Barracuda 320Gb
Sound Card
Onboard
Power Supply
Corsair HX 620W
Case
CM 690
CPU cooling
Tuniq Tower 120
GPU cooling
stock
OS
Vista Business and VMWare Ubuntu
Monitor
Acer AL2223W 22"
kdbolt70 is offline I fold for Overclock.net   Reply With Quote
Old 06-26-07   #8 (permalink)
4.0ghz
 
Knitelife's Avatar
 
intel ati

Join Date: Mar 2007
Location: Saint Louis, MO, USA
Posts: 827

Rep: 180 Knitelife is acknowledged by manyKnitelife is acknowledged by many
Unique Rep: 110
Trader Rating: 1
Default

Try changing:

Code:
request.KeepAlive = true;
to

Quote:
request.KeepAlive = false;

and see if that makes a difference.

System: Main Folding Rig
CPU
Q6600 @ 4.1Ghz
Motherboard
Asus P5K3-Deluxe
Memory
1GB Kingston DDR3-1375
Graphics Card
Rage 3D
Hard Drive
USB Jump Drive running Ubuntu
Power Supply
Silverstone Zues
Case
Lian-Li PC-60
CPU cooling
Vapochill Lightspeed
OS
Suse Enterprise Desktop 10.2
Monitor
KVM-Switch

Last edited by Knitelife : 06-26-07 at 06:04 PM.
Knitelife is offline Overclocked Account   Reply With Quote
Old 06-26-07   #9 (permalink)
The Project Keeper
 
Licht's Avatar
 
amd ati

Join Date: Mar 2007
Location: Bel Air
Posts: 9,377
Blog Entries: 3

Rep: 277 Licht is a proven memberLicht is a proven memberLicht is a proven member
Unique Rep: 184
Folding Team Rank: 874
Trader Rating: 0
Default

Never was much with networking in C#, or any other language. I say stay local! Lol.

I'm gonna stop posting in this thread before i embarrass myself.

System: (AMD-AMD-ATI)-(CPU-Chip-GPU)
CPU
AMD Athlon X2 5200+
Motherboard
Gigabyte 790X AM2+
Memory
6GB Kingston DDR2 667MHZ
Graphics Card
Saphire HD 3870
Hard Drive
[RAID0] [2X] WD 160GB 7,200RPM
Sound Card
X-Fi Extreme Gamer Fatality Professional
Power Supply
OCZ Game-X-Stream 700w
Case
NZXT Black Steel
CPU cooling
Stock Heatskin & Fan
GPU cooling
Stock Saphire 3870 Cooling
OS
Windows Vista Home Premium x64 SP1
Monitor
Samsung SyncMaster 19" Widescreen
Licht is offline I fold for Overclock.net Overclocked Account Licht's Gallery   Reply With Quote
Old 06-27-07   #10 (permalink)
Programmer
 
kdbolt70's Avatar
 
intel