HttpWebRequest的登录数据然后重定向 [英] HttpWebRequest Login data Then Redirect

查看:450
本文介绍了HttpWebRequest的登录数据然后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用HttpWebRequest和Httpwebresponse登录到通过POST一个网站,那么一旦通过认证有它重定向到一个默认页的新站点内。我可以做一个responsereader.ReadttoEnd()把我不确定如何得到一个自动重定向。

 昏暗ccContainer作为新的CookieContainer()
            昏暗的编码作为新ASCIIEncoding()
            昏暗strId作为字符串=用户名
            昏暗则strName作为字符串=密码 昏暗POSTDATA作为字符串=UAPMURL =安培; UAPMURLxx = XX&安培;登录名=&放大器; strId
        POSTDATA + =(与&密码1 =&放大器;则strName)
        昏暗的数据以字节()= encoding.GetBytes(POSTDATA)        'prepare网页的请求......
        昏暗myRequest由于HttpWebRequest的= DirectCast(WebRequest.Create(http://www.LOGINURLHERE.COM/LOGIN.PHP?),HttpWebRequest的)        昏暗的CC作为新的CookieContainer
        '&所述;&下; ---这是一天中的关键字
        myRequest.Method =POST
        myRequest.AllowAutoRedirect =假
        myRequest.ContentType =应用/的X WWW的形式urlen codeD
        myRequest.UserAgent =Mozilla的/ 5.0(兼容; MSIE 9.0; Windows NT的6.1;三叉戟/ 5.0)
        myRequest.KeepAlive = TRUE
        myRequest.CookieContainer =新的CookieContainer()        myRequest.ContentLength = data.Length
        方通暗淡作为流= myRequest.GetRequestStream()
        发送的数据。
        newStream.Write(数据,0,data.Length)
        newStream.Close()        昏暗_response由于HttpWebResponse = DirectCast(myRequest.GetResponse(),HttpWebResponse)
        如果myRequest.HaveResponse然后
            对于每个retCookie作为饼干在_response.Cookies
                cc.Add(retCookie)
            下一个
        万一        昏暗的要求,因为HttpWebRequest的= DirectCast(WebRequest.Create(http://www.DESTINATIONURL.COM/Main.php),HttpWebRequest的)
        request.CookieContainer = CC
        request.AllowAutoRedirect = FALSE
        昏暗_res由于HttpWebResponse = DirectCast(request.GetResponse(),HttpWebResponse)


如果我这样做...

注释掉这一行

 '昏暗_res作为HttpWebResponse = DirectCast(request.GetResponse(),HttpWebResponse)

使用这些......它读取并从目标URL中的数据填充我的当前页。我缺少的东西得到它的自动重定向?谢谢

 昏暗的要求,因为HttpWebRequest的= DirectCast(WebRequest.Create(http://www.destinationurl.com),HttpWebRequest的)
    request.CookieContainer = CC
    request.AllowAutoRedirect = FALSE 昏暗responseReader作为新的StreamReader(request.GetResponse()。GetResponseStream())
    昏暗responseData作为字符串= responseReader.ReadToEnd()
    responseReader.Close()
    的Response.Write(responseData)


解决方案

您没有自动跳转;服务器重定向。


  • 如果服务器向您发送重定向响应(code = 3XX),您请求重定向到的URL。

  • 如果重定向是透明处理,在 _response.ResponseURI 将包含地址,它重定向到。如果没有,你必须阅读重定向头,并决定自己是否要申请新的一页。

  • 如果服务器不重定向可言,你只需要申请任何你想要的网址,一旦你有你的身份验证cookie。

I'm trying to use HttpwebRequest and Httpwebresponse to log into a website via POST then once authenticated have it redirect to a default page within the new site. I'm able to do a responsereader.ReadttoEnd() put am unsure of how to get an automatic redirect.

     Dim ccContainer As New CookieContainer()
            Dim encoding As New ASCIIEncoding()
            Dim strId As String = "username"
            Dim strName As String = "password"

 Dim postData As String = "UAPMURL=&UAPMURLxx=xx&login=" & strId
        postData += ("&password1=" & strName)
        Dim data As Byte() = encoding.GetBytes(postData)

        ' Prepare web request...
        Dim myRequest As HttpWebRequest = DirectCast(WebRequest.Create("http://www.LOGINURLHERE.COM/LOGIN.PHP?"), HttpWebRequest)

        Dim cc As New CookieContainer


        ' <<--- This is the key word of the day
        myRequest.Method = "POST"
        myRequest.AllowAutoRedirect = False
        myRequest.ContentType = "application/x-www-form-urlencoded"
        myRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
        myRequest.KeepAlive = True
        myRequest.CookieContainer = New CookieContainer()

        myRequest.ContentLength = data.Length
        Dim newStream As Stream = myRequest.GetRequestStream()
        ' Send the data.
        newStream.Write(data, 0, data.Length)
        newStream.Close()

        Dim _response As HttpWebResponse = DirectCast(myRequest.GetResponse(), HttpWebResponse)
        If myRequest.HaveResponse Then
            For Each retCookie As Cookie In _response.Cookies
                cc.Add(retCookie)
            Next
        End If

        Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://www.DESTINATIONURL.COM/Main.php"), HttpWebRequest)
        request.CookieContainer = cc
        request.AllowAutoRedirect = False


        Dim _res As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)


If I do this ...

Comment out this line

 'Dim _res As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

Use these...it reads and fill my current page with the data from the destination URL. Am I missing something to get it to auto redirect? Thanks

   Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://www.destinationurl.com"), HttpWebRequest)
    request.CookieContainer = cc
    request.AllowAutoRedirect = False

 Dim responseReader As New StreamReader(request.GetResponse().GetResponseStream())
    Dim responseData As String = responseReader.ReadToEnd()
    responseReader.Close()
    Response.Write(responseData)

解决方案

You don't redirect; The server redirects.

  • If the server sends you a redirect response (code=3xx), you request the URL it redirects you to.
  • If the redirect is handled transparently, the _response.ResponseURI will contain the address it redirected to. If not, you have to read the redirect header and decide yourself whether or not to request the new page.
  • If the server doesn't redirect at all, you just need to request whatever URL you want once you have your authentication cookie.

这篇关于HttpWebRequest的登录数据然后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆