Reading text file from server

  • Thread starter Joe Ehrenfeld via .NET 247
  • Start date
J

Joe Ehrenfeld via .NET 247

I ma having an issue saving a text file that is located on a remove server (Chase Bank) to one of my servers. I know that I have the file in the Srtream object but I just can't seem to get it to save to my server. Here is my code:
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);

// Prepare web request...
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(chaseUrl);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.ContentLength = data.Length;
httpWebRequest.Timeout = 60000;

Stream readStream = httpWebRequest.GetRequestStream();

// Send the data.
readStream.Write(data, 0, data.Length);
readStream.Close();

HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
//Stream stream = httpWebResponse.GetResponseStream();
StreamReader stream = new StreamReader(httpWebRequest.GetResponse().GetResponseStream());

Console.WriteLine("Content Length: " + httpWebResponse.ContentLength.ToString());
Console.WriteLine("Content Encoding: " + httpWebResponse.ContentEncoding.ToString());
Console.WriteLine("Content Type: " + httpWebResponse.ContentType.ToString());
Console.WriteLine("Character Set: " + httpWebResponse.CharacterSet.ToString());
Console.WriteLine("Headers: " + httpWebResponse.Headers.ToString());
Console.WriteLine("Last Modified: " + httpWebResponse.LastModified.ToString());
Console.WriteLine("ResponseUri: " + httpWebResponse.ResponseUri.ToString());
Console.WriteLine("Server: " + httpWebResponse.Server.ToString());
Console.WriteLine("Status Code: " + httpWebResponse.StatusCode.ToString());
Console.WriteLine("Status Description: " + httpWebResponse.StatusDescription.ToString());

FileStream writeStream = new FileStream(destination, FileMode.OpenOrCreate, FileAccess.Write);

// Used with the StreamReader Object
Char[] readBuff = new Char[256];
byte [] buffer = new byte[256];
int count = stream.Read( readBuff, 0, 256 );

while (count > 0)
{
String outputData = new String(readBuff, 0, count);
writeStream.Write(buffer, 0, count);
//Console.WriteLine(outputData);
count = stream.Read(readBuff, 0, 256);
}
 
M

Matt Berther

Hello Joe Ehrenfeld via .NET 247,
// Used with the StreamReader Object
Char[] readBuff = new Char[256];
byte [] buffer = new byte[256];
int count = stream.Read( readBuff, 0, 256 );
while (count > 0)
{
String outputData = new String(readBuff, 0, count);
writeStream.Write(buffer, 0, count);
//Console.WriteLine(outputData);
count = stream.Read(readBuff, 0, 256);
}

Notice that you're doing writeStream.Write(buffer, 0, count);

also notice that you never assign buffer outside of the initial value.
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top