problem WebRequest and WebResponse



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

.



Relevant Pages

  • RE: problem WebRequest and WebResponse
    ... HttpWebRequest request = ... WebResponse response = null; ... System.Net.Sockets.SocketException: A connection attempt failed ... connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [OT] .NET Rant
    ... >>If you've got the raw request, why bother with HttpWebRequest at all? ... But if you want to specify the whole request and response yourself, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Read contents of a web page
    ... and retrieve the response from EndGetResponse. ... // This class stores the State of the request. ... HttpWebRequest request = state as HttpWebRequest; ... private static void RespCallback(IAsyncResult asynchronousResult) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how to handle socket timeout?
    ... My client code connects a TCP socket, and then uses it to send ... appear in this buffer after the timeout have happened. ... Or just wait for the response and process it when you get it, ... Also, with UDP, my server in its responce echoes the request ID that I ...
    (comp.unix.programmer)
  • "The underlying connection was closed: The request was canceled." on GetResponseStream
    ... request was canceled." ... Public HTTPDataObj As HTTPDataObject '-- HTTPDataObject is just a ... Public RequestObj As HttpWebRequest ... Dim Response As HttpWebResponse ...
    (microsoft.public.dotnet.languages.vb)

Loading