Hi,
I rewrite the code as you remcommend, but I either set the KeepAlive to
false or true, I still have the same problem.
Here's the code
-----------
using System;
using System.Xml;
using System.IO;
using System.Threading;
using System.Net;
namespace ReadXMLfromURL
{
class Class1
{
static void Main(string[] args)
{
TimerCallback callback = new TimerCallback(CaptureInfo);
Timer timer = new Timer(callback, null, 500, 1000*30);
System.Console.ReadLine();
timer.Dispose();
}
private static void CaptureInfo(object state)
{
String UrlString = "
http://www.worldpress.org/feeds/wprw.xml";
// Create a new HttpWebRequest object.Make sure that
// a default proxy is set if you are behind a fure wall.
HttpWebRequest myHttpWebRequest1 =
(HttpWebRequest)WebRequest.Create(UrlString);
myHttpWebRequest1.KeepAlive = false;
// Assign the response object of HttpWebRequest to a HttpWebResponse
variable.
HttpWebResponse myHttpWebResponse1 =
(HttpWebResponse)myHttpWebRequest1.GetResponse();
Stream streamResponse=myHttpWebResponse1.GetResponseStream();
String[] Str = new String[3];
int count = 0;
XmlTextReader reader = new XmlTextReader (streamResponse);
while (reader.Read() & (count<3))
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
if (reader.Name == "title")
{
reader.Read();
Str[count] = reader.Value;
count++;
}
Console.WriteLine("<" + reader.Name);
break;
}
//Console.WriteLine("depth =" + reader.Depth);
}
for (count=0;count<3;count++)
Console.WriteLine(Str[count]);
TextToFile myTextToFile = new TextToFile();
myTextToFile.writeToFile(Str);
}
}
//Write the information to the html file
public class TextToFile{
private const string FILE_NAME = "f:\\MyFile.html";
public void writeToFile(String[] Str)
{
StreamWriter sr = File.CreateText(FILE_NAME);
sr.WriteLine("<html>");
sr.WriteLine("</html>");
sr.Close();
}
}
}
-------------------------
Regards,
Girish Bharadwaj said:
I found that turning off "KeepAlive" (setting it to false) on the Request
seems to correct this problem. You might have to create a WebRequest
explicitly, set that flag to false, connect and then feed the
responsestream
to XmlTextReader.
--
Girish Bharadwaj
http://msmvps.com/gbvb
getting
back from the web server. This will hopefully give some insight as to what
is going wrong - what it is the web server doesn't like about your
request.