Re: problem WebRequest and WebResponse
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 12 Dec 2007 11:13:06 -0500
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Peter Bromberg [C# MVP]" <pbromberg@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:64458C66-387E-41CC-9A5B-313BB45E4132@xxxxxxxxxxxxxxxx
You should be able to do this very simply without alll that code:
XmlDocument doc = new XmlDocument();
doc.Load(strURL);
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"Juan Pablo MV" wrote:
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
.
- Follow-Ups:
- Re: problem WebRequest and WebResponse
- From: Juan Pablo MV
- Re: problem WebRequest and WebResponse
- References:
- problem WebRequest and WebResponse
- From: Juan Pablo MV
- RE: problem WebRequest and WebResponse
- From: Peter Bromberg [C# MVP]
- problem WebRequest and WebResponse
- Prev by Date: RE: call function from .NET Windows Service
- Next by Date: Re: SQLDataReader -- more questions
- Previous by thread: RE: problem WebRequest and WebResponse
- Next by thread: Re: problem WebRequest and WebResponse
- Index(es):
Relevant Pages
|
Loading