Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net HttpWebRequest未在投递时发送Cookie_Vb.net_Iis_Post_Cookies_Httpwebrequest - Fatal编程技术网

Vb.net HttpWebRequest未在投递时发送Cookie

Vb.net HttpWebRequest未在投递时发送Cookie,vb.net,iis,post,cookies,httpwebrequest,Vb.net,Iis,Post,Cookies,Httpwebrequest,我正在编写一个http模块作为反向代理,即从浏览器接收请求,将其发送到目标站点,接收响应并将其发送回浏览器 这一切都可以正常工作,除了在帖子中将cookie从浏览器请求转发到目标站点的问题。传出请求中的所有标题和表单数据都是正确的,但不包括cookie 我已经对从浏览器到IIS的请求和传出的httpwebrequest运行了fiddler,并证明了这一点。在debug中运行模块表明cookie在浏览器的请求中找到,并成功地放置在httpwebrequest的cookiecontainer中,但它

我正在编写一个http模块作为反向代理,即从浏览器接收请求,将其发送到目标站点,接收响应并将其发送回浏览器

这一切都可以正常工作,除了在帖子中将cookie从浏览器请求转发到目标站点的问题。传出请求中的所有标题和表单数据都是正确的,但不包括cookie

我已经对从浏览器到IIS的请求和传出的httpwebrequest运行了fiddler,并证明了这一点。在debug中运行模块表明cookie在浏览器的请求中找到,并成功地放置在httpwebrequest的cookiecontainer中,但它们只是没有出现在实际发出的请求中

如果我(在调试中)将outgoing request方法破解为Get,那么它们会去,但不会去发布

我还使用Fiddler跟踪了从浏览器直接到目标站点的请求/响应,在所有三种情况下(浏览器到目标、浏览器到我的IIS模块、IIS模块到目标),请求似乎都相同,只是IIS模块到目标忽略了cookie

下面是代码(VB.Net,并在2.0和4.5中试用):

注意:我尝试访问的域的形式是abc.domain.com(即它是子域,没有www),我尝试使用.domain.com表单的原因是响应中收到的cookie中使用的表单。我还尝试了其他组合,如abc.domain.com、.abc.domain.com等。我还尝试了创建Uri对象,并使用该方法将cookie添加到cookiecontainer中

我已经尝试了我能想到的一切,并能在论坛上找到…。有人有什么建议吗?我想我错过了一些明显的东西

当然,任何关于如何改进上述代码的其他评论都将不胜感激


谢谢。

好的,我发现了问题

我使用Fiddler查看http的运行情况,Fiddler是正确的。然而,当我使用Wireshark时,我发现http请求发送得比我想象的要早,而且只在Post上发送

原来是requestwriter.write行导致发送http请求,而不是GetResponse(Get就是这样)。因此,在requestwriter.write被发送之后,我在httpwebrequest中更改的任何内容都没有被发送

修正-我只是将所有的头和cookie设置移到requestwriter.write上面,一切都正常了

多么令人沮丧,但至少它现在已经修复了:)


如果有人对我是否犯了导致这种情况发生的错误有任何反馈,请让我知道。

自从发布这篇文章以来,我尝试了很多事情。相关的一点是,当我在GetResponse之前添加这行代码时:Dim strCookie作为String=reqTarget.CookieContainer.GetCookieHeader(reqTarget.RequestUri),strCookie包含我希望在请求中看到的cookie头!
' set up the request to the target
Dim reqTarget As System.Net.HttpWebRequest 
reqTarget = CType(System.Net.HttpWebRequest.Create(strTargetURL & strTargetPath & qstring), System.Net.HttpWebRequest)

' copy relevant info, cookies etc from the application request to the target request
CopyAppRequest(application.Context.Request, reqTarget)

' send the request and get the response
Dim rspTarget As System.Net.HttpWebResponse = CType(reqTarget.GetResponse(), System.Net.HttpWebResponse)


Private Sub CopyAppRequest(ByRef reqApp As System.Web.HttpRequest, ByRef reqTarget As System.Net.HttpWebRequest)

    ' copy over the headers
    For Each key As String In reqApp.Headers.AllKeys
        Select Case key
            Case "Host", "Connection", "Content-Length", "Accept-Encoding", "Expect",  "Authorization", "If-Modified-Since"
                ' not sure if we need to process these
            Case "Connection"
                reqTarget.Connection = reqApp.Headers(key)
            Case "Content-Type"
                reqTarget.ContentType = reqApp.Headers(key)
            Case "Accept"
                reqTarget.Accept = reqApp.Headers(key)
            Case "Referer"
                reqTarget.Referer = reqApp.Headers(key)
            Case "User-Agent"
                reqTarget.UserAgent = reqApp.Headers(key)
            Case "Cookie"
                ' do nothing, cookies are handled below..
            Case Else
                reqTarget.Headers.Add(key, reqApp.Headers(key)
        End Select
    Next

    reqTarget.Method = reqApp.HttpMethod
    reqTarget.AllowAutoRedirect = False

    If reqTarget.Method = "POST" Then
        reqTarget.ContentLength = reqApp.ContentLength
        Dim datastream() As Byte = System.Text.Encoding.UTF8.GetBytes(reqApp.Form.ToString)
        reqTarget.ContentLength = datastream.Length
        Dim requestwriter As System.IO.Stream = reqTarget.GetRequestStream
        requestwriter.Write(datastream, 0, datastream.Length)
        requestwriter.Close()
        requestwriter.Dispose()
    End If

    Dim CookieJar As New System.Net.CookieContainer
    reqTarget.CookieContainer = CookieJar

    For Each key As String In reqApp.Cookies.AllKeys
        Dim tgtCookie As New System.Net.Cookie
        With tgtCookie 
            .Name = reqApp.Cookies.Item(key).Name
            .Value = reqApp.Cookies.Item(key).Value
            .Domain = ".domain.com"
            .Path = "/"
            .Expires = DateAdd(DateInterval.Month, 1, System.DateTime.Now)
            .HttpOnly = True
      End With
        CookieJar.Add(tgtCookie)
    Next

End Sub