.NET Framework - Reading from a controller by HTTP

Asked By Sonnich Jensen on 08-Mar-12 10:36 AM
I have tried a number of ways and I get this error:

The server committed a protocol violation. Section=ResponseStatusLine

I am reading a CSV file from a deprag controller running linux.

My code on win7 is as follows:



textBox2.Text = "starting...";
try
{
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(textBox1.Text);
textBox1.Text = http://200.200.200.101/cgi-bin/cgiread?site=13&dlfile=/mnt/mmc/finaldata/Actual.csv

// Set some reasonable limits on resources used by
this request
request.ServicePoint.Expect100Continue = false;
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;

WebResponse response =
(WebResponse)request.GetResponse(); // crashes here
fromcache.Checked = response.IsFromCache;

long length = response.ContentLength;
string contentType = response.ContentType;

// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();

// Pipes the stream to a higher level stream reader
with the required encoding format.
StreamReader readStream = new
StreamReader(receiveStream, Encoding.UTF8);

label2.Text = readStream.ReadToEnd();

response.Close();
readStream.Close();

textBox2.Text = "completed!";
}
catch (Exception ee)
{
textBox2.Text = ee.Message;
}




Sonnich Jensen replied to Sonnich Jensen on 08-Mar-12 10:47 AM
=3D/mnt/mmc/finald...
s used by
=3D false;
4;
4;
e;
pe;
ponse.
onseStream();
eam reader

and the file read contains of one line only:

06.10.2009,10:22:16,1,1,2.1,0,0.0,0,0,0.0,0,0,0.0,0,1,0.0,1437,0,0/0,Ncm,de=
g

works in any browser
bradbury9 replied to Sonnich Jensen on 08-Mar-12 10:58 AM
El jueves 8 de marzo de 2012 16:47:57 UTC+1, Sonnich Jensen  escribi=F3:
e=3D/mnt/mmc/finald...
ces used by
=3D false;
=3D 4;
=3D 4;
che;
Type;
esponse.
sponseStream();
tream reader
deg

I would:
- Check http server log to check if the respose status code is the expected=
(200).
- Sniff http request and server response to find out strange things

Seems like the server is not returning a valid http status code or a not su=
pported one.

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
bradbury9 replied to bradbury9 on 08-Mar-12 11:52 AM
El jueves 8 de marzo de 2012 16:58:47 UTC+1, bradbury9  escribi=F3:
ne
ile=3D/mnt/mmc/finald...
urces used by
e =3D false;
=3D 4;
=3D 4;
Cache;
h;
ntType;
response.
ResponseStream();
stream reader
);
m,deg
ed (200).
supported one.

You can check the .net framework supported status codes at msdn: http://msd=
n.microsoft.com/en-us/library/system.net.httpstatuscode.aspx

Proxy settings and user autentification are usually things to take into con=
sideration that can make IE and FF showing content right and .NET giving ex=
ceptions. But I think it is a differet one because of the "protocol violati=
on. Section=3DResponseStatus" message.
Arne_Vajhøj replied to Sonnich Jensen on 08-Mar-12 05:18 PM
You use textBox1.Text before giving it a value??

Arne
Sonnich Jensen replied to Arne_Vajhøj on 09-Mar-12 06:04 AM
e=3D/mnt/mmc/finald...

No, it has the value given here - as you see the quotes are missing.
The textbox is so I can use my range of Deprag's, I have 10 of them
with different IPs....
Sonnich Jensen replied to bradbury9 on 09-Mar-12 06:50 AM
=F3:
Line
lfile=3D/mnt/mmc/finald...
sources used by
nue =3D false;
s =3D 4;
h =3D 4;
omCache;
gth;
tentType;
he response.
etResponseStream();
el stream reader
d();
Ncm,deg
cted (200).
t supported one.
dn.microsoft.com/en-us/library/system.net.httpstatuscode.aspx
onsideration that can make IE and FF showing content right and .NET giving =
exceptions. But I think it is a differet one because of the "protocol viola=
tion. Section=3DResponseStatus" message.

Well, the GetResponse crashes, so I never get any code.
The value does not get set, so I dont get any data....
bradbury9 replied to Sonnich Jensen on 09-Mar-12 07:16 AM
El viernes 9 de marzo de 2012 12:50:07 UTC+1, Sonnich Jensen  escribi=F3:
i=F3:
usLine
&dlfile=3D/mnt/mmc/finald...
resources used by
tinue =3D false;
ons =3D 4;
gth =3D 4;
FromCache;
ength;
ontentType;
the response.
.GetResponseStream();
evel stream reader
End();
0,Ncm,deg
pected (200).
not supported one.
msdn.microsoft.com/en-us/library/system.net.httpstatuscode.aspx
consideration that can make IE and FF showing content right and .NET givin=
g exceptions. But I think it is a differet one because of the "protocol vio=
lation. Section=3DResponseStatus" message.

That is the reason I told you to check the server logs or sniff the http tr=
afic between the client and the server ;-)
Bjørn_Brox replied to Sonnich Jensen on 09-Mar-12 07:42 AM
Den 09.03.2012 12:04, skrev Sonnich Jensen:

Have you tried using WebClient and let the OS take care of the low-level
communication?


try
{
using (WebClient wc = new WebClient())
label2.Text = wc.DownloadString(textBox1.Text);
}
catch (WebException ex)
{
textBox2.Text = ex.Message;
}

--
Bj?rn Brox
Bjørn_Brox replied to Bjørn_Brox on 09-Mar-12 07:46 AM
Den 09.03.2012 13:42, skrev Bj?rn Brox:

I would also have checked if the URL is correct, because the argument
dlfile=/mnt/mmc/finaldata/Actual.csv soes not seem to be a Linux file
path (which would use \mnt\mmc\... )


--
Bj?rn Brox
bradbury9 replied to Bjørn_Brox on 09-Mar-12 07:55 AM
=20

You are wrong. Linux path delimiter is / and you tell him it is \ so file p=
ath would be /mnt/mmc/...

May be strange, but is one thing I like about Linux, Unix and BSD OS's.


BTW: that absolute path looks like a nasty security bug.
Bjørn_Brox replied to bradbury9 on 09-Mar-12 08:05 AM
Den 09.03.2012 13:55, skrev bradbury9:
You are correct.


Absolutely

--
Bj?rn Brox
Bjørn_Brox replied to bradbury9 on 09-Mar-12 08:21 AM
Den 09.03.2012 13:55, skrev bradbury9:
...

Sorry, I mixed a bit.


Absolutely, but I doubt that any webserver would give root access, which
again raises the question: Are you sure that the path are correct, -
seen from the outside?

--
Bj?rn Brox
bradbury9 replied to Bjørn_Brox on 09-Mar-12 08:37 AM
=20

/mnt/mmc/ seems to be some sort of card reader device mount point and most =
likely it would be mounted with big privileges (unless they did chgroup the=
mount point and made it rw for apache user). If they do:

File.ReadAllBytes(dlfile);

And it works with that path you have a potencial security bug :-D
Arne_Vajhøj replied to Sonnich Jensen on 09-Mar-12 09:52 PM
OK.

That tricked me.

Arne
Arne_Vajhøj replied to Bjørn_Brox on 09-Mar-12 09:56 PM
Yep.

But even if it had been the case then one usually use / in
URL's and translate to OS specific path server side. Also
for file name query parameters.

Arne