C# Newbie

G

Guest

Hello,
i am trying to make an Https request to a coradiant box. the box monitors
perforamnce of our website. when i paste the url into IE i am asked to accept
a ceritificate. then i get a dialog box asking where to save the file. I have
written a C# application. the will make the https request and i am stroring
the responces in a stream. but i cant read the stream i get an error the
stream does not support seek operations. here is url and the code that i am
have written can someone tell me whats i amdoing wrong thanks

https://truesight/wpdata?usr=xxxx&pwd=xxxxx&timeframe=fullday&format=CSV&zip=false

internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
public AcceptAllCertificatePolicy()
{
}

public bool CheckValidationResult(ServicePoint sPoint,
X509Certificate cert, WebRequest wRequest,int certProb)
{
// Always accept
return true;
}
}


private void btnGetData_Click(object sender, System.EventArgs e)
{
try

{
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
string rq =
System.Configuration.ConfigurationSettings.AppSettings.GetValues("httpReq").GetValue(0).ToString().Replace("%20","&");
//X509Certificate Cert =
X509Certificate.CreateFromCertFile("192.168.0.199");
X509Certificate Cert =
X509Certificate.CreateFromCertFile(@"C:\\Documents and Settings\afloyd\My
Documents\Truesight\TSDateExtractor\bin\Debug\ts.cer");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(rq);
//req.ClientCertificates
req.ClientCertificates.Add(Cert);
req.UserAgent = "Client Cert Sample";
req.Method = "GET";

HttpWebResponse Response = (HttpWebResponse)req.GetResponse();

Stream ReceiveStream = Response.GetResponseStream();

byte[] bytes = new byte[1000];
int numBytesToRead = 5000;
int numBytesRead = 500;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = ReceiveStream.Read(bytes, numBytesRead, numBytesToRead);
// The end of the file is reached.
if (n==0)
break;
numBytesRead += n;
numBytesToRead -= n;
}


ReceiveStream.Close();

}

catch (System.Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}

}
 
G

Guest

Alvis,

Stream ReceiveStream = Response.GetResponseStream();

Shouldn't that be:

Stream ReceiveStream = Request.GetResponseStream();

Also, if the stream isn't very big, you should be able to read it in one go:

byte[] bytes = new byte[ReceiveStream.Length]; // or something to that
effect.


Peter




--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Alvis said:
Hello,
i am trying to make an Https request to a coradiant box. the box monitors
perforamnce of our website. when i paste the url into IE i am asked to accept
a ceritificate. then i get a dialog box asking where to save the file. I have
written a C# application. the will make the https request and i am stroring
the responces in a stream. but i cant read the stream i get an error the
stream does not support seek operations. here is url and the code that i am
have written can someone tell me whats i amdoing wrong thanks

https://truesight/wpdata?usr=xxxx&pwd=xxxxx&timeframe=fullday&format=CSV&zip=false

internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
public AcceptAllCertificatePolicy()
{
}

public bool CheckValidationResult(ServicePoint sPoint,
X509Certificate cert, WebRequest wRequest,int certProb)
{
// Always accept
return true;
}
}


private void btnGetData_Click(object sender, System.EventArgs e)
{
try

{
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
string rq =
System.Configuration.ConfigurationSettings.AppSettings.GetValues("httpReq").GetValue(0).ToString().Replace("%20","&");
//X509Certificate Cert =
X509Certificate.CreateFromCertFile("192.168.0.199");
X509Certificate Cert =
X509Certificate.CreateFromCertFile(@"C:\\Documents and Settings\afloyd\My
Documents\Truesight\TSDateExtractor\bin\Debug\ts.cer");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(rq);
//req.ClientCertificates
req.ClientCertificates.Add(Cert);
req.UserAgent = "Client Cert Sample";
req.Method = "GET";

HttpWebResponse Response = (HttpWebResponse)req.GetResponse();

Stream ReceiveStream = Response.GetResponseStream();

byte[] bytes = new byte[1000];
int numBytesToRead = 5000;
int numBytesRead = 500;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = ReceiveStream.Read(bytes, numBytesRead, numBytesToRead);
// The end of the file is reached.
if (n==0)
break;
numBytesRead += n;
numBytesToRead -= n;
}


ReceiveStream.Close();

}

catch (System.Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}

}
 
G

Guest

thanks peter i see the error of my ways!

Peter Bromberg said:
Alvis,

Stream ReceiveStream = Response.GetResponseStream();

Shouldn't that be:

Stream ReceiveStream = Request.GetResponseStream();

Also, if the stream isn't very big, you should be able to read it in one go:

byte[] bytes = new byte[ReceiveStream.Length]; // or something to that
effect.


Peter




--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Alvis said:
Hello,
i am trying to make an Https request to a coradiant box. the box monitors
perforamnce of our website. when i paste the url into IE i am asked to accept
a ceritificate. then i get a dialog box asking where to save the file. I have
written a C# application. the will make the https request and i am stroring
the responces in a stream. but i cant read the stream i get an error the
stream does not support seek operations. here is url and the code that i am
have written can someone tell me whats i amdoing wrong thanks

https://truesight/wpdata?usr=xxxx&pwd=xxxxx&timeframe=fullday&format=CSV&zip=false

internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
public AcceptAllCertificatePolicy()
{
}

public bool CheckValidationResult(ServicePoint sPoint,
X509Certificate cert, WebRequest wRequest,int certProb)
{
// Always accept
return true;
}
}


private void btnGetData_Click(object sender, System.EventArgs e)
{
try

{
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
string rq =
System.Configuration.ConfigurationSettings.AppSettings.GetValues("httpReq").GetValue(0).ToString().Replace("%20","&");
//X509Certificate Cert =
X509Certificate.CreateFromCertFile("192.168.0.199");
X509Certificate Cert =
X509Certificate.CreateFromCertFile(@"C:\\Documents and Settings\afloyd\My
Documents\Truesight\TSDateExtractor\bin\Debug\ts.cer");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(rq);
//req.ClientCertificates
req.ClientCertificates.Add(Cert);
req.UserAgent = "Client Cert Sample";
req.Method = "GET";

HttpWebResponse Response = (HttpWebResponse)req.GetResponse();

Stream ReceiveStream = Response.GetResponseStream();

byte[] bytes = new byte[1000];
int numBytesToRead = 5000;
int numBytesRead = 500;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = ReceiveStream.Read(bytes, numBytesRead, numBytesToRead);
// The end of the file is reached.
if (n==0)
break;
numBytesRead += n;
numBytesToRead -= n;
}


ReceiveStream.Close();

}

catch (System.Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}

}
 

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