473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Handling Remote Server Errors

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);
}
}
}
Aug 14 '07 #1
1 1335
grif wrote:
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);
}
}
}

Just put a try/catch block around this line:

HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

Then log the error (or do whatever you want with it) and then use
continue to get out of the loop and process the next line from the file.

--
-glenn-
Aug 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave Smithz | last post by:
Hi There, I have taken over someone else's PHP code and am quite new to PHP. I made some changes and have implemented them to a live environment fine so far. However, I now want to setup a...
1
by: Céline | last post by:
HI everybody, I am trying to get somewhere with VisualStudio.net. The problem is that my web server is not on my PC (for company security reason) on is remote. Everytime I try to create a...
9
by: Marina Anufreichik | last post by:
Hi, After deploymnet web application on web server I can access page on local machine and login fine but when I'm trying to access web site from remote machine I can see login page, but when I'm...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
4
by: aaj | last post by:
Hi all I have an automated application, that runs in the middle of the night. If certain 'non system' errors occur (things like malformed files, missing files etc..), I send an automatic Email...
1
by: Metal Dave | last post by:
I do not understand the error handling of SQL Server here. Any error in bulk insert seems to halt the current T-SQL statement entirely, rendering it impossible to log an error. The first statement...
3
by: dgiagio | last post by:
Hi, I'm creating a SMTP application and I would like to hear opinions about error handling. Currently there are two functions that communicate with the remote peer: ssize_t...
2
by: tshad | last post by:
This has been driving me crazy. I have been trying to get the error handling working on my system and can get parts of it working and others won't work at all. I found that you can't access...
0
by: acnx | last post by:
I have an ntier application. I am trying to determine what is the best practice for handing errors in a datagrid. My datagrids are able to add, update and delete data. I am using a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.