刚才发了一个贴,我要做的是用TcpClient和NetworkStream用Post方式上传参数,然后获得返回的下载文件,并且保存到本地。(该下载是采用post方式的)现在我已经成功的发送Post,并且保存了文件,文件是Torrent格式(BT文件),不过文件头上带着Http返回的一些信息。我仅仅是想知道怎样可以把这些Http返回信息从流中过滤掉再保存到文件。这是我的代码 TcpClient tcpClient = new TcpClient();
Uri URI = new Uri("http://www1.westdown.com/download/down.php");
tcpClient.Connect(URI.Host, URI.Port);
NetworkStream netStream = tcpClient.GetStream();
if (netStream.CanWrite)
{
string msg = "type=torrent&id=XX00&name=ADODB";
StringBuilder sb = new StringBuilder();
sb.Append("POST " + URI.PathAndQuery + " HTTP/1.0\r\n");
sb.Append("Host: " + URI.Host + "\r\n");
sb.Append("Content-Type: application/x-www-form-urlencoded\r\n");
sb.Append("Content-Length: " + Encoding.UTF8.GetByteCount(msg) + "\r\n");
sb.Append("\r\n");
sb.Append(msg + "\r\n"); Byte[] sendBytes = Encoding.UTF8.GetBytes(sb.ToString());
netStream.Write(sendBytes, 0, sendBytes.Length);
netStream.Flush();
} if(netStream.CanRead)
{
FileStream fs=new FileStream("e:\\test.torrent",FileMode.Create);
byte[] block=new byte[1024];
while(true)
{
int blocklen=netStream.Read(block,0,block.Length); //MessageBox.Show(blocklen.ToString()); if(blocklen==0) break; fs.Seek(0,SeekOrigin.End);
fs.Write(block,0,blocklen); } fs.Close(); } tcpClient.Close();
netStream.Close();
下载回来的test.torrent中有HTTP/1.1 302 Moved Temporarily
Cache-Control: private
Content-Length: 23144
Content-Type: application/force-download
Location: http://www1.westdown.com/download/file.php/XX00.html
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.2.8
Content-Disposition: attachment; filename="ADODB.torrent"
Date: Sat, 31 Dec 2011 16:14:37 GMT
Connection: close
这样的内容。。手动编辑把这段去掉后。Torrent文件完全正常
Uri URI = new Uri("http://www1.westdown.com/download/down.php");
tcpClient.Connect(URI.Host, URI.Port);
NetworkStream netStream = tcpClient.GetStream();
if (netStream.CanWrite)
{
string msg = "type=torrent&id=XX00&name=ADODB";
StringBuilder sb = new StringBuilder();
sb.Append("POST " + URI.PathAndQuery + " HTTP/1.0\r\n");
sb.Append("Host: " + URI.Host + "\r\n");
sb.Append("Content-Type: application/x-www-form-urlencoded\r\n");
sb.Append("Content-Length: " + Encoding.UTF8.GetByteCount(msg) + "\r\n");
sb.Append("\r\n");
sb.Append(msg + "\r\n"); Byte[] sendBytes = Encoding.UTF8.GetBytes(sb.ToString());
netStream.Write(sendBytes, 0, sendBytes.Length);
netStream.Flush();
} if(netStream.CanRead)
{
FileStream fs=new FileStream("e:\\test.torrent",FileMode.Create);
byte[] block=new byte[1024];
while(true)
{
int blocklen=netStream.Read(block,0,block.Length); //MessageBox.Show(blocklen.ToString()); if(blocklen==0) break; fs.Seek(0,SeekOrigin.End);
fs.Write(block,0,blocklen); } fs.Close(); } tcpClient.Close();
netStream.Close();
下载回来的test.torrent中有HTTP/1.1 302 Moved Temporarily
Cache-Control: private
Content-Length: 23144
Content-Type: application/force-download
Location: http://www1.westdown.com/download/file.php/XX00.html
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.2.8
Content-Disposition: attachment; filename="ADODB.torrent"
Date: Sat, 31 Dec 2011 16:14:37 GMT
Connection: close
这样的内容。。手动编辑把这段去掉后。Torrent文件完全正常
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货