This is my code am trying to make a stream from the url and and i want to cut file into 6 pieces and download , because of that i want to connect the stream to file info but am getting some errors. thanks
FileInfo fs;
DateTime startTime = DateTime.UtcNow;
WebRequest request = WebRequest.Create("http://tegos.ru/new/mp3_full/David_Guetta_feat_Ne-Yo_and_Akon_-_Play_Hard.mp3");
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
using (Stream fileStream = fs new FileInfo("")))
error 'System.IO.FileInfo' is a 'type' but is used like a 'variable'
Assuming you want to copy the stream from your HTTP response to a file:
using (Stream output = File.OpenWrite("pah_to_file"))
{
using (Stream input = http.Response.GetResponseStream())
{
byte[] buffer = new byte[2048]; // some buffer
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
If it's not the case, do rephrase your question.
Related
I am using HttpWebRequest.GetStreamData() to get data from stream and then writing a couple of files in that variable.
Before calling the GetResponse() method on the stream, I need to get the copy of data and put it in text format.
Here's my code snippet
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.Method = "POST";
string requestbody = "Some Body Text"
Stream rs = wr.GetRequestStream();
foreach (fragment in Documents).ToList())
{
byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(requestbody);
rs.Write(requestBytes, 0, requestBytes.Length);
FileStream fileStream = new FileStream(fragment.FilePath, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Flush();
fileStream.Close();
fileStream = null;
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("Trailer Text");
rs.Write(trailer, 0, trailer.Length);
}
}
using (StreamReader strm = new StreamReader(rs))
{
string text = strm.ReadToEnd();
}
rs.Close();
rs = null;
The issue is I get error "stream was not readable".
When I check the property rs.canread, it is set to false.
Can anyone help me reading the content of my rs stream.
I have tried a couple of methods but none seems to be working as of now.
My question is about create a browser that recognize a streaming video and then save it on a local directory.
I can use c#, Someone can help me and give me some advise.
At the beginning I think that I can use c# and an array of byte to get stream but I have some problems of implementation because I cannot found the steam.
This is the code:
public void StreamDownload(string currentUrl,string fileath)
{
int dataLength;
int bytesRead;
var filePath = System.IO.Path.Combine(fileath, "test");
if (File.Exists(filePath))
File.Delete(filePath);
WebRequest req = WebRequest.Create(currentUrl);
WebResponse response = req.GetResponse();
string oFileName = System.IO.Path.GetFileName("test");
Stream dataStream = response.GetResponseStream();
byte[] buffer = new byte[1024];
FileStream oFile = new FileStream(oFileName, FileMode.Append);
dataLength = (int)response.ContentLength;
do
{
bytesRead = dataStream.Read(buffer, 0, buffer.Length);
oFile.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
File.WriteAllBytes(filePath, buffer);
}
The problem is that it save HTML page and not the stream
wait some experts XD
thank you.
I download a file with this method:
WebClient WC = new WebClient();
WC.DownloadFile(url, filePath);
And i want that in the same time to read the file in the same time with:
var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
but i get allways :
The process cannot access the file 'filePath' because it is being used by another process.
It's possible to download and read in the same time?
Edit
I now download the file with:
var req = (HttpWebRequest)HttpWebRequest.Create(url);
var fileStream = new FileStream(filePath,
FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
using (var resp = req.GetResponse())
{
using (var stream = resp.GetResponseStream())
{
byte[] buffer = new byte[0x10000];
int len;
while ((len = stream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, len);
}
}
}
And still get the error....
var req = (HttpWebRequest)HttpWebRequest.Create(url);
using (var resp = req.GetResponse())
{
using (var stream = resp.GetResponseStream())
{
byte[] buffer = new byte[0x10000];
int len;
while ((len = stream.Read(buffer, 0, buffer.Length))>0)
{
//Do with the content whatever you want
// ***YOUR CODE***
fileStream.Write(buffer, 0, len);
}
}
}
Try this way :
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://www.domain.com/file.txt");
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
var stream = response.GetResponseStream();
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.Read(buffer, 0, 8192)) > 0)
{
// handle your raw data
// you needed raw data = buffer[0 to bytesRead - 1]
}
stream.Close();
response.Close();
I am using code below to upload large file to server and noticed that copying FileStream to GetRequestStream the bytes array is created and hold in memory. This increase large object heap and I don't want it. Maybe someone know how to solve this?
Stream formData = new FileStream(.....)
HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
using (Stream requestStream = request.GetRequestStream())
{
Helpers.CopyStream(formData, requestStream);
requestStream.Close();
}
public static void CopyStream(Stream fromStream, Stream toStream)
{
try
{
int bytesRead;
byte[] buffer = new byte[32768];
while (fromStream != null && (bytesRead = fromStream.Read(buffer, 0, buffer.Length)) > 0)
{
toStream.Write(buffer, 0, bytesRead);
}
}
catch (IOException)
{
//suppress empty stream response
}
}
Memory profiler graph. bytes array create internally in GetRequestStream
You can use the HttpWebRequest.AllowWriteStreamBuffering to disable internal buffering:
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.AllowWriteStreamBuffering = false;
using (Stream formData = File.Open(fileName, FileMode.Open))
using (Stream requestStream = request.GetRequestStream())
{
formData.CopyTo(requestStream, 32768);
}
I'm trying to download a .torrent file (not the contents of the torrent itself) in my .NET application.
Using the following code works for other files, but not .torrent. The resulting files is about 1-3kb smaller than if I download the file via a browser. When opening it in a torrent client, it says the torrent is corrupt.
WebClient web = new WebClient();
web.Headers.Add("Content-Type", "application/x-bittorrent");
web.DownloadFile("http://kat.ph/torrents/linux-mint-12-gnome-mate-dvd-64-bit-t6008958/", "test.torrent");
Opening the URL in a browser results in the file being downloaded correctly.
Any ideas as to why this would happen? Are there any good alternatives to WebClient that would download the file correctly?
EDIT: I've tried this as well as WebClient, and it results in the same thing:
private void DownloadFile(string url, string file)
{
byte[] result;
byte[] buffer = new byte[4096];
WebRequest wr = WebRequest.Create(url);
wr.ContentType = "application/x-bittorrent";
using (WebResponse response = wr.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (MemoryStream memoryStream = new MemoryStream())
{
int count = 0;
do
{
count = responseStream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
} while (count != 0);
result = memoryStream.ToArray();
using (BinaryWriter writer = new BinaryWriter(new FileStream(file, FileMode.Create)))
{
writer.Write(result);
}
}
}
}
}
The problem that server returns content compressed by gzip and you download this compressed content to file. For such cases you should check the "Content-Encoding" header and use proper stream reader to decompress the source.
I modified your function to handle gzipped content:
private void DownloadFile(string url, string file)
{
byte[] result;
byte[] buffer = new byte[4096];
WebRequest wr = WebRequest.Create(url);
wr.ContentType = "application/x-bittorrent";
using (WebResponse response = wr.GetResponse())
{
bool gzip = response.Headers["Content-Encoding"] == "gzip";
var responseStream = gzip
? new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)
: response.GetResponseStream();
using (MemoryStream memoryStream = new MemoryStream())
{
int count = 0;
do
{
count = responseStream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
} while (count != 0);
result = memoryStream.ToArray();
using (BinaryWriter writer = new BinaryWriter(new FileStream(file, FileMode.Create)))
{
writer.Write(result);
}
}
}
}