RE: problem WebRequest and WebResponse

Tech-Archive recommends: Fix windows errors by optimizing your registry



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


.



Relevant Pages

  • Re: Problems with access to a web page
    ... Server: Apache ... Connection: close ... Look what I get now when I send the exact same request ... and got exactly the same 0 length response ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • problem WebRequest and WebResponse
    ... I have a problem using HttpWebRequest so I wish somebody can help me. ... HttpWebRequest request = ... WebResponse response = null; ... connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& ...
    (microsoft.public.dotnet.languages.csharp)
  • MULTITHREADED
    ... where each client request are handle ... connected socket return by accept function. ... X Server close the connection without passing all the requested data ...
    (comp.programming.threads)
  • Re: socket questions
    ... TcpClient socket; ... > one does not (send while waiting for a response from a previous ... > an event with the server response as the event argument. ... each thread generating a connection? ...
    (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)