problem WebRequest and WebResponse

J

Juan Pablo MV

Hi,

I have a problem using HttpWebRequest so I wish somebody can help me.

1. Application: I have a windows service that gets a XML through a
http (URL) from my provider (feeder). Once I get the XML I process it
and store the XML's information in a database.

2. The following method is the one that I have been using to get the
XML:

public XmlDocument GetXMLHttp(string strURL)
{

// URL request
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(strURL); ;
request.Method = "POST";
request.Timeout = 10000; // 10 secs
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = false;
request.UserAgent = ".NET Framework Example Client";

XmlDocument rssDoc = new XmlDocument();
WebResponse response = null;
Stream dataStream = null;
try
{
// I use the default credentials
request.Credentials =
CredentialCache.DefaultCredentials;
//' Getting answer.
response = request.GetResponse();
//' Opening the answer stram
dataStream = response.GetResponseStream();
//' Read content
rssDoc.Load(dataStream);
//' Close stream.
dataStream.Close();
dataStream.Dispose();
//' Close WebResponse.
response.Close();
response = null;
//' Close HttpWebRequest.
request.Abort();
request = null;


}
catch
{
//' Close stream.
if (dataStream != null) dataStream.Close();
if (dataStream != null) dataStream.Dispose();

//' Close WebResponse.
if (response != null) response.Close();
response = null;

if (request != null) request.Abort();
request = null;


WebProxy proxyObject = new WebProxy("192.168.0.2",
8080);
proxyObject.Credentials = new
NetworkCredential("usuario", "clave", "dominio");
proxyObject.BypassProxyOnLocal = true;
WebRequest.DefaultWebProxy = proxyObject;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.KeepAlive = false;
Stream rssStream = null;
// Getting answer from the URL
try
{
response = request.GetResponse();
rssStream = response.GetResponseStream();
rssDoc.Load(rssStream);

//' Close stream.
rssStream.Close();
rssStream.Dispose();
//' Close WebResponse.
response.Close();
response = null;
//' Close HttpWebRequest.
request.Abort();
request = null;

}
catch (WebException e)
{
//' Close stream.
if (rssStream != null) rssStream.Close();
if (rssStream != null) rssStream.Dispose();

//' Close WebResponse.
if (response != null) response.Close();
response = null;

if (request != null) request.Abort();
request = null;

if
(Convert.ToBoolean(Dictionary(AppDomain.CurrentDomain.BaseDirectory.ToString()
+ "\\XML\\Config.xml", "Log", "/Config/AppConfig")))
{
// Sending email with the error found
System.IO.StreamWriter myFile = new
System.IO.StreamWriter("C:\\error\\errorLectura" +
DateTime.Now.ToString("MMddyyyy HHmmssfff") + ".txt");
myFile.WriteLine(e);
myFile.Close();
}
}
}
return rssDoc;
}


3. Now, this service is working fine for a short period of time. It
gets data from the URL and process it fine, but after a couple of
hours I am getting the following error:

System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: A connection attempt failed
because the connected party did not properly respond after a period
of
time, or established connection failed because connected host has
failed to respond
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,
SocketAddress socketAddress)
at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean
connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress&
address, ConnectSocketState state, IAsyncResult asyncResult, Int32
timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at MarcadorCORE.MarcadorXMLUtils.GetXMLHttp(String strURL)


4. I am trying everything I can do but for some reason I have been
getting the same error so I will appreciate your help in this matter.

Best regards,

Juan
 
N

Nicholas Paldino [.NET/C# MVP]

Not only that, but the OP should ^really^ be using using statements for
the Stream and the WebResponse instances (if he wants to go that way). The
reason for this is that if there is an exception in the catch statement when
calling Close on one of the resources, it will terminate the execution of
the catch clause and then not dispose of the instances correctly.

When using using, it will expand to multiple try/finally statements
which will ensure that all IDisposable implementations will be cleaned up
correctly. This code is definitely not doing that.

Also, there is a great deal of repeated code which should most
definitely be refactored, which would probably help with finding the issue.
 
J

Juan Pablo MV

it tries with

XmlDocument doc = new XmlDocument();
doc.Load(strURL);

, and until the moment this working very well. and the method is much
more small. Thanks!!! and thanks Nicholas your contributes is very
important also.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top