I am having trouble with the HttpWebRequest.GetRequestStream method.
I am posting data to my web server running NT4 iis4 using a loop. So
every time the loop executes I want it to post data to my web page.
It work the first 2 times the loop iterates, but on the third time
around it gives me a timout error.
The line it gives me an error on is:
--Stream newStream = myRequest.GetRequestStream();--
Could someone please point me in the right direction.
Many thanks!
Form Code:
private void button1_Click(object sender, System.EventArgs e)
{
int x;
x=0;
while(x < 1)
{
this.textBox1.Text = "Sleeping";
Application.DoEvents();
Thread.Sleep(3000);
HRATS_Functions X = new HRATS_Functions();
try
{
X.PostData();
X.Dispose();
//this.textBox2.Text = X.myResponse.ToString();
}
catch (Exception a)
{
this.textBox1.Text = "Exception: " + a.ToString();
break;
}
this.textBox1.Text = "Awake";
Application.DoEvents();
Thread.Sleep(3000);
}
}
Class Code:
public HttpWebRequest myRequest =
(HttpWebRequest)Request.Create("http://lab/collectdata.asp");
public ASCIIEncoding encoding = new ASCIIEncoding();
public string myResponse;
public void PostData()
{
// Initialize the WebRequest parameters
string postData = "First_Name=Matt";
byte[] byte1 = encoding.GetBytes(postData);
//Set Request Properties
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postData.Length;
myRequest.Timeout = 5000;
//send the request
Stream newStream = myRequest.GetRequestStream();
newStream.Write(byte1,0,byte1.Length);
newStream.Flush();
newStream.Close();
}