Need help for dotNet

  • Thread starter Richard Blewett [DevelopMentor]
  • Start date
R

Richard Blewett [DevelopMentor]

Can you connect to the resource from a browser in the office?

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog


Hi,
I write a C# to get information from a XML site, when I run my program at
home, there's no problem.
But when in office (no proxy, no firewall, port 80 is open), I've got this
error:
<snip>
 
R

Richard Blewett [DevelopMentor]

Use TcpTrace (www.pocketsoap.com) to see what HTTP error you are 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.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Yes, of course, I can connect the resource from a browser.
 
G

Girish Bharadwaj

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
Richard Blewett said:
Use TcpTrace (www.pocketsoap.com) to see what HTTP error you are 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.
 
B

Binh

Hi,
I write a C# to get information from a XML site, when I run my program at
home, there's no problem.
But when in office (no proxy, no firewall, port 80 is open), I've got this
error:

---------------
Unhandled Exception: System.Net.WebException: The underlying connection was
closed: An unexpected error occurred on a receive.
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.HttpWebRequest.GetResponse()
at ReadXMLfromURL.Class1.CaptureInfo(Object state) in c:\Documents and
Settings\Binh.EEE\Local

settings\Temp\SnippetCompilerTemp\c60a33a1-29ba-4601-9d92-3bae54b7720f\0:line
32
--------------



This is the code
--------------------

using System;

using System.Xml;

using System.IO;



namespace ReadXMLfromURL

{

class Class1

{

static void Main(string[] args)

{

String URLString =
"http://www.worldpress.org/feeds/wprw.xml";

String[] Str = new String[3];

int count = 0;



XmlTextReader reader = new XmlTextReader
(URLString);



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;

}



}

for (count=0;count<3;count++)

Console.WriteLine(Str[count]);

}



}



}

-------------------------------



Appreciate if you can help.

Thanks.
 
G

Girish Bharadwaj

What version of .NET are you using is it 1.0,1.1 or 1.1 with SP1? Also try
to add
myHttpWebRequest1.Accept = "*/*";
as a header.

--
Girish Bharadwaj
http://msmvps.com/gbvb
phangmo said:
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.
 
P

phangmo

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();
}
}

}
 

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