- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道这个话题已经讨论过很多次了,但我需要了解如何以正确的方式编写代码。
我在协议(protocol)版本 HTTP 1.1 中多次使用相同的 HttpWebRequest(到相同的 url)。
Method = "POST"
KeepAlive = True
但每次我需要发送不同的请求,并得到不同的响应。
Private Sub SendHttpWebReq()
Dim httpWebReq = CType(Net.WebRequest.Create("http://www.contoso.com/"), Net.HttpWebRequest)
httpWebReq.Method = "POST"
httpWebReq.KeepAlive = True
httpWebReq.ContentType = "application/x-www-form-urlencoded"
Dim myRequestString As New List(Of String) From {"abc", "def"}
Dim ContentList As New List(Of String)
For a = 0 To 1
Dim inputData As String = MyRequestString(a)
Dim postData As String = "firstone" + ChrW(61) + inputData
Dim encoding As New System.Text.ASCIIEncoding()
Dim byteData As Byte() = encoding.GetBytes(postData)
httpWebReq.ContentLength = byteData.Length
Dim newStream As IO.Stream = httpWebReq.GetRequestStream()
newStream.Write(byteData, 0, byteData.Length)
newStream.Flush()
newStream.Dispose()
Dim Response As Net.WebResponse = httpWebReq.GetResponse()
Dim ResponseStream As Io.Stream = Response.GetResponseStream()
Dim Content = New Io.MemoryStream()
ResponseStream.CopyTo(Content)
Response.Close()
Response.Dispose()
ResponseStream.Flush()
ResponseStream.Dispose()
ContentList.Add(System.Text.Encoding.UTF8.GetString(Content.ToArray))
Content = Nothing
Next
End Sub
当我运行代码时,我第一次得到了正确的响应,但是当我尝试重用 HttpWebRequest 时,在这一行抛出了一个异常:
httpWebReq.ContentLength = byteData.Length
异常是写入开始后无法设置此属性
KeepAlive
它指的是:连接,还是请求?
Dim httpWebReq = CType(Net.WebRequest.Create("http://www.contoso.com/"), Net.HttpWebRequest)
我应该创建一个 HttpWebRequest 类的实例,但我应该用这个指令建立连接:
Dim newStream As IO.Stream = httpWebReq.GetRequestStream()
我对么?
最佳答案
这是我认为需要澄清的地方,因为该声明可能被认为具有误导性,其措辞方式如下:
1 WebRequest => 1 WebResponse. You can't change anything in a WebRequest once it has been initialized.
You can't change any parameter of a WebRequest after the request has been issued and a WebResponse has been returned, until after the WebResponse is closed (disposed).
WebResponse
已经返回一个结果,它可以被关闭 - 显式或隐式(在
Using
块中) - 你可以请求另一个,根据需要修改
WebRequest
参数(例如,将方法从 POST 更改为 GET)。
WebRequest
必须在同一过程中多次重新初始化,以接收不确定数量的 WebResponse,直到到达目标地址(或登录页面)。
--------------
(GET or POST) | WebRequest | (Method is POST)
|---------> | GET/(POST) | <-----------| <-------------- |
| -------------- | |
| | | |
-------------- --------------- ------------------ --------------
| New | | WebResponse |--> | LogIn Required |-->| LogIn |
| Location | --------------- ------------------ | Address |
| (Referer | | --------------
| Set) | |
-------------- (Set Cookies)
| |
| ---------------
| | LogIn |
Redirection <----| OK |---NO---|
--------------- |
| |
YES |
(Set Cookies) |
| Request
--------------- Denied
| Response | |
| URI | |
--------------- |
| |
EXIT <------------|
|
请注意,发出 WebRequest,如果访问请求的资源 URI 需要身份验证,则服务器可能不会以
StatusCode 回答。
302 (Found)
,
301 (Moved)
或
303 (Redirected)
,它可能只是将 StatusCode 设置为
200 (OK)
.重定向是隐式的,因为设置了“位置” header ,或者如果是 WebForm 登录,则检索到的 Html 页面包含重定向。
Hops
,这通常必须手动处理(以验证我们是否被发送到我们真正想去的地方)。
keep-alive
标题 .
keep-alive
header 由客户端和/或服务器设置为
提示 成立的对口
连接应该保持开放,至少一段时间,因为与当前交易相关的其他资源被交换的可能性很高。
Http
和
Ftp
请求,这是
Http 1.1
中的标准.
keep-alive
header 指的是
连接 已经通过 WebRequest 建立的,而不是 WebRequest 本身。当一个连接被创建并指定它应该保持打开时,后续的请求应该维护
connection: keep-alive
符合协议(protocol)的 header 。
HttpWebRequest
,设置
KeepAlive
属性(property)到
False
, 相当于设置
connection: close
标题。
(...) when you call 1 new HttpWebRequest every 3 seconds, every 10seconds, or every 60 seconds? What's the difference when i send thoserequests with True or False?
KeepAlive
属性(property)到
提示 建立连接的连接管理器应该保持打开状态,因为您知道它将被重新使用。但是,
ServicePointManager
如果使用的协议(protocol)提供此设置,并且在管理连接池复合体的内部逻辑所施加的限制内,则远程服务器将遵守该请求。
HTTP 1.0
, 这是
HTTP 1.1
中的默认设置, 在
HTTP 2.0
中被忽略.
keep-alive
,因为到请求资源的路由中的某些设备(特别是代理)可能需要明确设置此设置(阅读有关代理及其行为的 IETF 文档)。
200 (OK)
收到或请求被拒绝或我们被重定向太多次(取消 token 在这里也可能有用)。
Public Async Sub SomeMethodAsync()
LoginParameters = New LoginObject() With {
.CookieJar = New CookieContainer,
.LogInUrl = "[Some IP Address]",
.Credentials = New Dictionary(Of String, String)
}
LoginParameters.Credentials.Add("UserName", "[Username]")
LoginParameters.Credentials.Add("Email", "[email]")
LoginParameters.Credentials.Add("Password", "[Password]")
LoginParameters = Await HttpLogIn(LoginParameters)
7End Sub
LoginParameters
必须保留对象,因为引用了
CookieContainer
,其中包含认证后收到的 Cookie。当一个新的 WebRequest 被初始化时,这些 Cookies 被传递到服务器,作为请求的凭证已经被认证的“证明”。请注意,这些 Cookie 会在一段时间后过期(这些 Cookie 在发出新的 WebRequest 时会“刷新”,除非 Session 有时间限制)。如果是这种情况,登录过程会自动重复。
Imports System.Net
Imports System.Net.Security
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.Text
Public LoginParameters As LoginObject
Public Class LoginObject
Public Property LogInUrl As String
Public Property ResponseUrl As String
Public Property Credentials As Dictionary(Of String, String)
Public Property StatusCode As HttpStatusCode
Public Property CookieJar As New CookieContainer()
End Class
Public Async Function HttpLogIn(LogInParameters As LoginObject) As Task(Of LoginObject)
Dim httpRequest As HttpWebRequest
Dim StatusCode As HttpStatusCode
Dim MaxHops As Integer = 20
' Windows 7 (.Net 4.5.1+ required):
'ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
' Windows 10 (.Net 4.5.1+ required):
ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault
'If needed or for testing
'ServicePointManager.ServerCertificateValidationCallback = AddressOf CertificateValidation
httpRequest = WebRequest.CreateHttp(LogInParameters.LogInUrl)
Try
HTTP_RequestHeadersInit(httpRequest, String.Empty, LogInParameters.CookieJar)
Using httpResponse As HttpWebResponse = CType(Await httpRequest.GetResponseAsync(), HttpWebResponse)
StatusCode = httpResponse.StatusCode
End Using
If StatusCode = HttpStatusCode.OK OrElse StatusCode = HttpStatusCode.NoContent Then
'POST Parameters are URLEncoded and the encoded strings converted to a Byte array of UTF8 chars
Dim EncodedParameters As Byte() = HTTP_EncodePOSTParameters(LogInParameters.Credentials)
httpRequest = WebRequest.CreateHttp(LogInParameters.LogInUrl)
httpRequest.Method = WebRequestMethods.Http.Post
httpRequest.ContentType = "application/x-www-form-urlencoded"
httpRequest.ContentLength = EncodedParameters.Length
HTTP_RequestHeadersInit(httpRequest, String.Empty, LogInParameters.CookieJar)
Using stream As Stream = Await httpRequest.GetRequestStreamAsync()
stream.Write(EncodedParameters, 0, EncodedParameters.Length)
End Using
Dim Hops As Integer = 0
Dim Referer As String = LogInParameters.LogInUrl
Dim LastHttpMethod As String = httpRequest.Method
Do
'Evaluate Authentication redirect or page moved
Using httpResponse As HttpWebResponse = CType(Await httpRequest.GetResponseAsync(), HttpWebResponse)
StatusCode = httpResponse.StatusCode
LogInParameters.ResponseUrl = URIFromResponseLocation(httpResponse).ToString()
End Using
If (StatusCode = HttpStatusCode.Moved) OrElse
(StatusCode = HttpStatusCode.Found) OrElse
(StatusCode = HttpStatusCode.RedirectMethod) OrElse
(StatusCode = HttpStatusCode.RedirectKeepVerb) Then
httpRequest = WebRequest.CreateHttp(LogInParameters.ResponseUrl)
HTTP_RequestHeadersInit(httpRequest, Referer, LogInParameters.CookieJar)
If StatusCode = HttpStatusCode.RedirectKeepVerb Then
httpRequest.Method = LastHttpMethod
Else
LastHttpMethod = httpRequest.Method
End If
End If
If (CType(StatusCode, Integer) > 320) OrElse Hops >= MaxHops Then
Exit Do
End If
Hops += 1
Loop While (StatusCode <> HttpStatusCode.OK)
If StatusCode = HttpStatusCode.OK Then
LogInParameters.CookieJar = httpRequest.CookieContainer
End If
End If
Catch exW As WebException
StatusCode = If(exW.Response IsNot Nothing,
CType(exW.Response, HttpWebResponse).StatusCode,
CType(exW.Status, HttpStatusCode))
Catch exS As System.Exception
StatusCode = CType(WebExceptionStatus.RequestCanceled, HttpStatusCode)
Finally
ServicePointManager.ServerCertificateValidationCallback = Nothing
End Try
LogInParameters.StatusCode = StatusCode
Return LogInParameters
End Function
Private Sub HTTP_RequestHeadersInit(ByRef httpReq As HttpWebRequest, Referer As String, CookiesJar As CookieContainer)
httpReq.Date = DateTime.Now
httpReq.CookieContainer = CookiesJar
httpReq.KeepAlive = True
httpReq.ConnectionGroupName = Guid.NewGuid().ToString()
httpReq.AllowAutoRedirect = False
httpReq.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
httpReq.ServicePoint.Expect100Continue = False
httpReq.Referer = Referer
httpReq.UserAgent = "Mozilla/5.0 (Windows NT 10; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0"
httpReq.Accept = "ext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
httpReq.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US;q=0.9,en;q=0.5")
httpReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate;q=0.8")
httpReq.Headers.Add(HttpRequestHeader.CacheControl, "no-cache")
End Sub
Private Function HTTP_EncodePOSTParameters(PostParameters As Dictionary(Of String, String)) As Byte()
Dim Encoder As New System.Text.UTF8Encoding()
Dim CredentialValues As New StringBuilder()
Dim _first As Boolean = True
For Each CurrentKeyPair As KeyValuePair(Of String, String) In PostParameters
If _first = False Then CredentialValues.Append("&")
CredentialValues.AppendFormat("{0}={1}", WebUtility.UrlEncode(CurrentKeyPair.Key),
WebUtility.UrlEncode(CurrentKeyPair.Value))
_first = False
Next
Return Encoder.GetBytes(CredentialValues.ToString())
End Function
Private Function URIFromResponseLocation(Response As HttpWebResponse) As System.Uri
Dim uri As Uri
Dim Location As String = Response.Headers("Location")
Try
If uri.IsWellFormedUriString(Location, UriKind.Absolute) Then
uri = New Uri(Location, UriKind.Absolute)
Else
Dim HostUri As String = Response.ResponseUri.GetComponents(UriComponents.SchemeAndServer,
UriFormat.Unescaped) + Location
uri = If(uri.IsWellFormedUriString(HostUri, UriKind.Absolute),
New Uri(HostUri),
New Uri(Response.ResponseUri.GetComponents(UriComponents.Scheme, UriFormat.Unescaped) +
Response.ResponseUri.Host + Location))
End If
Catch ExceptionOnInvalidUri As Exception
uri = New Uri(Location, UriKind.Relative)
End Try
Return uri
End Function
Private Function CertificateValidation(sender As Object,
CACert As X509Certificate,
CAChain As X509Chain,
PolicyErrors As SslPolicyErrors) As Boolean
'This method, as it is, accepts a Server certificate in any case
'It could be eventually adapted to refuse a connection (returning false)
'if the certificate is invalid, expired or from a untrusted path
If (PolicyErrors = SslPolicyErrors.None) Then Return True
'If a Certificated must be added to the Chain, uncomment the code below,
'selecting a Certificate in the Local (or other) Storage
'Dim MyCert As X509Certificate2 = New X509Certificate2("[localstorage]/[ca.cert]")
'CAChain.ChainPolicy.ExtraStore.Add(MyCert)
'CAChain.Build(MyCert)
'For Each CACStatus As X509ChainStatus In CAChain.ChainStatus
' If (CACStatus.Status <> X509ChainStatusFlags.NoError) And
' (CACStatus.Status <> X509ChainStatusFlags.UntrustedRoot) Then
' Return False
' End If
'Next
Return True
End Function
关于.net - 了解KeepAlive模式下的HttpWebRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49554203/
我正在使用 HttpWebRequest 登录页面并获取一些信息。然后,我使用该信息创建一个新的 HttpWebRequest 以获取更多信息。我不想使用 WebClient。 如何将使用第一个 Ht
我有一个包含指向某些文件的链接的页面。 我基本上需要访问页面的源代码来解析它并获取文件的所有超链接。 我的代码是这样的(我在网上很多地方都找到了一些代码..): "private static
我正在编写一个渐进式下载器作为可移植类库 (Profile=24)。它将支持以字节 block 的形式部分下载目标文件。 HttpClient 不可用,我将使用 HttpWebRequest,它具有用
我正在努力在 HttpWebRequest 之上构建一个流畅的 REST 客户端界面。/HttpWebResponse .NET 中的类型。到目前为止,一切都很好......但是我正在尝试开发一个可插
我有一个用 VB.NET 编写的应用程序( 不是 asp.net,它是一个 Windows 控制台应用程序)。我正在尝试调用一个 url(一个 html 页面)并将响应返回到一个字符串中。响应是直接的
我尝试使用 C# HTTPWebRequest 类登录 amazon.com,我可以登录但无法读取 header 中的多个 set-cookie 当服务器响应多个“Set-Cookie:”-heade
我正在测试 Web 核心 API,但收到 500 内部服务器错误。 我的 Controller 上的方法是; [Route("api/property")] public class Property
假设我正在检索一个 url,如下所示: string url = "http://www.somesite.com/somepage.html" HttpWebRequest req = (HttpW
当我使用 HttpWebRequest.Headers.Add("Cookie",value) 与 HttpWebRequest.CookieContainer 和结果从 HttpWebRequest
我已阅读以下 2 篇文章并尝试实现相同的文章。 我的代码是这样的,超时发生在这里 HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
我正在寻找一种使用 httpWebRequest 从 url 下载 excel 文件并以某种方式解析它的方法 - 这是否意味着将其转换为 .csv 文件,以便我可以简单地使用 TextFieldPar
当我尝试序列化 HttpWebRequest 时出现以下错误 Type 'System.Net.KnownHttpVerb' in Assembly 'System, Version=2.0.0.0,
我目前正在下载一个 HTML 页面,使用以下代码: Try Dim req As System.Net.HttpWebRequest = DirectCast(WebRequest.Creat
我读了这个 MSDN 喜欢它并运行它的例子。 http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.useragent.a
我有一个 ASP.NET MVC 操作,它通过 HttpWebRequest 将 GET 请求发送到另一台服务器。我想在新请求中包含原始操作请求中的所有 cookie。原始请求中的一些 System.
我正在尝试抓取具有用户身份验证的网站。我能够执行POST来发送登录信息并存储Cookie。但是,登录后,尝试访问 protected 页面时出现403错误。 $url = "https://some_
我正在使用 HttpWebRequest ,并且正在处理响应流。 HttpWebRequest是否有正确的处理方法? ,因为它不包含 close 或 dispose 方法? 最佳答案 如果该类有特殊的
我在REST服务中抛出一个错误,例如: throw new WebFaultException("bla bla bla", HttpStatusCode.HttpVersionNotSuppo
问题:此控制台应用调用 Azure 上托管的长时间运行的网页两次。我希望它只调用一次。 控制台应用程序因捕获的异常而失败:基础连接已关闭:接收时发生意外错误。 so question 如果我从 Chr
我试图弄清楚我的网络请求在最终到达最终内容之前被重定向了多少次。 我正在创建我的网络请求,如下所示: var httpRequest = (HttpWebRequest) WebRequest.Cre
我是一名优秀的程序员,十分优秀!