ダウンロードしたすべてのバイトを 3 つの異なるファイルに書き込もうとしています。現在、WebRequest オブジェクトと WebResponse オブジェクトを使用しています。私はそれが正しい方法だと確信していますか?部分的にファイルにデータを書き込む部分で行き詰まりました。どのデータが書き込まれるかに関係なく、現在の目的は、同じストリームからデータを読み取り、3 つの異なるファイルに書き込むことです。エラーが発生するよりも、最初のファイルに正常に書き込むことができます-ストリーム(response.getResponseStream()から取得した)を別のバイナリリーダーに割り当てようとすると、ストリームが読み取れません。
私はそれを2つの方法で試しました.1つは、応答ストリームを異なるバイナリリーダーに直接渡すことです.失敗しました. 次に、バイナリリーダーごとに個別の参照を作成しようとしましたが、これも失敗しました。それが役立つ場合のコードは次のとおりです:-
using (Stream strm = res.GetResponseStream())
{
using (Stream strm1 = strm)
{
int i = 0;
BinaryReader br = new BinaryReader(strm1);
br.BaseStream.BeginRead(buffer, 0, buffer.Length,
new AsyncCallback(ProcessDnsInformation), br);
Console.WriteLine("Data read {0} times", i++);
Console.ReadKey();
File.WriteAllBytes(@"C:\Users\Vishal Sheokand\Desktop\Vish.bin", buffer);
br.Close();
}
using (Stream strm2=strm)
{
int i = 0;
BinaryReader br = new BinaryReader(strm2);
br.BaseStream.BeginRead(buffer, 0, buffer.Length,
new AsyncCallback(ProcessDnsInformation), br);
Console.WriteLine("Data read {0} times", i++);
Console.ReadKey();
File.WriteAllBytes(@"C:\Users\Vishal Sheokand\Desktop\Vish1.bin", buffer);
br.Close();
}
using (Stream strm3 = strm)
{
int i = 0;
BinaryReader br = new BinaryReader(strm3);
br.BaseStream.BeginRead(buffer, 0, buffer.Length,
new AsyncCallback(ProcessDnsInformation), br);
Console.WriteLine("Data read {0} times", i++);
File.WriteAllBytes(@"C:\Users\Vishal Sheokand\Desktop\Vish2.bin", buffer);
br.Close();
}
}
私は C# を学んでいます。愚かなコーディングの一部 (またはすべて) を無視してください。