Send/Receive Data to server in C#
In my programe I have a need for requesting data from开发者_StackOverflow中文版 server and upon collecting the response, use the returned data for further processing.
I am using "system.net.webrequest" class for the same. I have few questions on this:
- after I receive the data (data is of 0.5MB size in minimum) I have to process this entire data to derive some result. I was thinking to store the returned data in a "txt" file and then read the data from that file and process.
Is this a better idea? If not, can you please suggest.
I am making post request to several servers (>10 servers) and using data from them. I observed that for some reason if the server is not reachable then my programe stops throwing an exception
Server Not Reachable
A first chance exception of type 'System.Net.WebException' occurred in System.dll
How can I solve this problem? Any idea?
Appreaciate your help.
Thanks, Rahul
With regards to the exception; implement a try
/catch
region, and take appropriate action (perhaps logging the error and queuing it for a retry in a few moments).
I'm not entirely sure of what the first question is since we don't know what the data/system is - but some thoughts:
- to reduce bandwidth, if the server supports compression, make sure you are telling it - set the accept-header accordingly (
request.Accept = "gzip,deflate";
on your request, if usingHttpWebRequest
); or maybe a denser serialization format if you control both ends of the pipe - make sure you are making appropriate use of things like
Dictionary<,>
when processing the data; I can't tell you what "appropriate" is without knowing what "processing" or "the data" means in your specific context ;p
Saving the data to a file is only necessary if you don't want to, or cannot, process it as you receive it or if you want to keep that data around after processing for debugging purposes. I typically avoid creating files like this if I can as it adds to the maintenance overhead.
To continue processing data from other servers in case of an exception on one, you will need to use a Try/Catch
block as follows (pseudo):
foreach (var server in serverList)
{
try
{
... fetch data and process ...
}
catch (Exception ex)
{
// handle exception (log, requeue, etc)
}
}
精彩评论