Weird problem reading XML from URL
Here's my code:
string url = @"http://eafsys:1234/vp.xml";
try
{
XmlTextReader reader = new XmlTextReader(url);
while (reader.Read())
{
// do whatever..
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
When I run it I catch the following error:
'The server committed a protocol violation. Section=ResponseStatusLine'
I searched the internet and apparently I am supposed to change some web.config file in my IIS. The thing is the system where the XML data comes from doesn't have IIS, it doesn't even have a web server. It's just a program listening on that port waiting to output XML.
I can surf (with a any browser, I.E or FireFox) to http://eafsys:1234/vp.xml and I get nicely formatted XML.
Here is the raw (source view) of the XML:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<EAFSTATUS>
<EAST>
<HEAT_NO>100633</HEAT_NO>
<GRADE>EA174 </GRADE>
<HEATSTATUS>MELT</HEATSTATUS>
<CHARGENUM>2</CHARGENUM>
<CRMTEMP>1657</CRMTEMP>
<CRMC> 0.0519</CRMC>
<CHARGE>178000</CHARGE>
<ENERGY>80710</ENERGY>
<ELECTKWH>46400</ELECTKWH>
<POWERONTIME> 33:18</POWERONTIME>
<POWERSTATUS>OFF</POWERSTATUS>
<DELAYDUR>3:18</DELAYDUR>
<DELAYSTATUS>OPEN </DELAYSTATUS>
<DELAYREASON> NO REASON </DELAYREASON>
</EAST>
<WEST>
<HEAT_NO>100632</HEAT_NO>
<GRADE>EA174 </GRADE>
<HEATSTATUS>REF </HEATSTATUS>
<CHARGENUM>2</开发者_开发问答CHARGENUM>
<CRMTEMP>1529</CRMTEMP>
<CRMC> 0.0418</CRMC>
<CHARGE>178500</CHARGE>
<ENERGY>95010</ENERGY>
<ELECTKWH>54500</ELECTKWH>
<POWERONTIME> 39:06</POWERONTIME>
<POWERSTATUS>OFF</POWERSTATUS>
<DELAYDUR></DELAYDUR>
<DELAYSTATUS></DELAYSTATUS>
<DELAYREASON></DELAYREASON>
</WEST>
<LMF_EAST>
<HEAT_NO>100631</HEAT_NO>
<GRADE>EA719 </GRADE>
<HEATSTATUS>DONE</HEATSTATUS>
<LMF_TIME> 58:48</LMF_TIME>
<TEMP>1546</TEMP>
<WT>163100</WT>
<SAMP>M03</SAMP>
<SAMP_S>0.005760</SAMP_S>
<CAO>0</CAO>
<AL2O3>0</AL2O3>
</LMF_EAST>
<LMF_WEST>
<HEAT_NO>100632</HEAT_NO>
<GRADE>EA174 </GRADE>
<HEATSTATUS>REF </HEATSTATUS>
<LMF_TIME> 47:42</LMF_TIME>
<TEMP>1566</TEMP>
<WT>167500</WT>
<SAMP>M02</SAMP>
<SAMP_S>0.000000</SAMP_S>
<CAO>0</CAO>
<AL2O3>0</AL2O3>
</LMF_WEST>
<CASTER>
<HEATNUM>100631</HEATNUM>
<OUTBOARDHEATNUM>0</OUTBOARDHEATNUM>
<CASTSPEED>1.295000</CASTSPEED>
<CASTWIDTH>1.365312</CASTWIDTH>
<REMAININGWEIGHT>66714</REMAININGWEIGHT>
<NEXTTIME>15:20:13.00</NEXTTIME>
<HEATDUR>55</HEATDUR><LADLESINSERVICE>4</LADLESINSERVICE></CASTER>
</EAFSTATUS>
If anyone has any idea's I'd love to hear them.
When you tell a browser to open a file with a url you are leveraging the local file (or even network) system. The browser actually is capable of opening the file and reading it. When you tell an XmlTextReader to open a file with a URL you are telling it to issue an httpRequest and expect an httpResponse in return, and that requires an internet server of some flavor.
You might be able to substitute a filestreamreader and then feed that stream to the XmlTextReader, but I haven't tried that. You are likely better of servicing that file from an web server or accessing it via a UNC instead of a URL.
I had a similar error once, although I don't remember whether it was exactly this. Anyway, you can try the solution that worked for me: setting the following property before you download anything:
ServicePointManager.Expect100Continue = false;
精彩评论