简体   繁体   English

如何从响应中读取标头并使用c#取消所有其他标头?

[英]how to read only headers from response and cancel all the rest using c#?

a server that i want to work with it return 302 Found with some body bytes here is the raw 我要使用它的服务器返回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 to cancel it from being downloaded : 我想取消下载此主体字节:

<!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>

here is my code manupulating the response using asynchorous request : 这是我的代码使用异步请求处理响应:

  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

the output is null but the response is fully downloaded ! 输出为null,但响应已完全下载! and i want to skip this i want only headers and cancel all the rest of the response stream 我想跳过这个,我只想要标题,然后取消所有其余的response stream

so is there any method to read only the headers and cancel all the remaning bytes 有没有办法只读取标题并取消所有剩余字节

To read headers you should check response.Headers collection, without calling GetResponseStream. 要读取标题,您应该检查response.Headers集合,而不调用GetResponseStream。 Have you tried this, is entire body downloaded anyway? 您是否尝试过了,还是下载了全身吗?

Another thing you may try - is to request data using 'HEAD' request. 您可以尝试的另一件事-是使用“ HEAD”请求来请求数据。 It is specifically designed to retrieve only headers, always without body. 它是专门为仅在没有主体的情况下仅检索标头而设计的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM