G
grif
Hey Everyone!
Ok time for another newbie question from me...I've written a little program
that reads URL's from a text file line by line and then does a POST action
etc The problem is that the program gets an exception and terminates when a
remote server gives an error like....method not allowed etc
Any ideas how or what I can use to avoid it terminating the program
completely???
Heres the code i'm using etc...dont worry about the TERRIBLE formatting or
me not putting it into classes etc...i'm still learning and I just banged it
together as sort of an idea...ill properly structure it once I know I can
actually do it lol...phew got that out of the way...now for the code:
static void Main(string[] args)
{
// set variables
int ctr = 0;
string read = null;
int test = 0;
// Open the file for reading
StreamReader tr = File.OpenText("url.txt");
while ( test == 0)
{
//increment submission counter
ctr++;
// grab the line
read = tr.ReadLine();
// assign result to variable
string url = read;
//print variable
System.Console.WriteLine("the current url is:{0}", url);
// ****** Start Remote POST PROCEDURES *****
//Our postvars
byte[] buffer =
Encoding.ASCII.GetBytes("?blabla=somevariable&some=somevarialbe");
// Initialise HTTP Request and Method
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
// Set WebRequest Method ie post or Get
WebReq.Method = "POST";
// Set the content typeof the form
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always
important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Let's show some information about the response
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());
if (read == "end")
{
test++;
}
}
// close the text readerstream
tr.Close();
System.Console.WriteLine("We have submitted to {0} forms", ctr);
}
}
}
Ok time for another newbie question from me...I've written a little program
that reads URL's from a text file line by line and then does a POST action
etc The problem is that the program gets an exception and terminates when a
remote server gives an error like....method not allowed etc
Any ideas how or what I can use to avoid it terminating the program
completely???
Heres the code i'm using etc...dont worry about the TERRIBLE formatting or
me not putting it into classes etc...i'm still learning and I just banged it
together as sort of an idea...ill properly structure it once I know I can
actually do it lol...phew got that out of the way...now for the code:
static void Main(string[] args)
{
// set variables
int ctr = 0;
string read = null;
int test = 0;
// Open the file for reading
StreamReader tr = File.OpenText("url.txt");
while ( test == 0)
{
//increment submission counter
ctr++;
// grab the line
read = tr.ReadLine();
// assign result to variable
string url = read;
//print variable
System.Console.WriteLine("the current url is:{0}", url);
// ****** Start Remote POST PROCEDURES *****
//Our postvars
byte[] buffer =
Encoding.ASCII.GetBytes("?blabla=somevariable&some=somevarialbe");
// Initialise HTTP Request and Method
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
// Set WebRequest Method ie post or Get
WebReq.Method = "POST";
// Set the content typeof the form
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always
important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Let's show some information about the response
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());
if (read == "end")
{
test++;
}
}
// close the text readerstream
tr.Close();
System.Console.WriteLine("We have submitted to {0} forms", ctr);
}
}
}