如何使用c#从响应中读取头,并取消所有其余部分
本文关键字:取消 余部 何使用 响应 读取 | 更新日期: 2023-09-27 18:10:40
一个服务器,我想用它返回302 Found
与一些身体字节这里是原始的
HTTP/1.1 302 Found
Server: unknown
Date: Sun, 29 Jun 2014 20:12:14 GMT
Content-Type: text/html; charset=utf-8
Connection: close
P3P: CP="CAO PSA OUR"
x-powered-by:
Set-Cookie: session=604d0bdba04eb54793ec2f3c98b2a37e; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: www.mysite.com/login.php?session=604d0bdba04eb54793ec2f3c98b2a37e
Vary: Accept-Encoding
Content-Length: 18163
this body bytes i want cancel from download:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
///////body bytes about 18kb///////
</html>
下面是我使用异步请求操作响应的代码:
Dim request As HttpWebRequest = CType(asynchronousResult.AsyncState, HttpWebRequest)
Dim BYTES_TO_READ As Integer = 0
Dim buffer = New Byte(BYTES_TO_READ - 1) {}
Using response As HttpWebResponse = CType(request.EndGetResponse(asynchronousResult), HttpWebResponse)
Using sm As Stream = response.GetResponseStream()
Dim totalBytesRead As Integer = 0
Dim bytesRead As Integer
Do
bytesRead = sm.Read(buffer, totalBytesRead, BYTES_TO_READ - totalBytesRead)
totalBytesRead += bytesRead
Loop While totalBytesRead < BYTES_TO_READ
request.Abort()
response.Close()
sm.Close()
End Using
End Using
Dim s = Encoding.Default.GetString(buffer)
Console.WriteLine(s)
Catch ex As WebException
Exit Sub
End Try
输出为空,但响应已完全下载!我想跳过这个,我只想要标题,并取消所有其余的response stream
那么是否有方法可以只读取头并取消所有剩余的字节
要读取标头,您应该检查响应。集合,而不调用GetResponseStream。你试过了吗,整个身体都下载了吗?
你可以尝试的另一件事是使用'HEAD'请求请求数据。