powershell httpwebrequest GET method cookiecontainer problem?

后端 未结 3 1546
时光取名叫无心
时光取名叫无心 2020-12-15 11:11

I\'m trying to scrape a website that has user authentication. I am able to do a POST to send my login and stores a cookie. However, after the login I get a 403 error when tr

3条回答
  •  心在旅途
    2020-12-15 11:23

    People have been asking for the complete application, here you have it

    $url = "https://some_url"
    
    $CookieContainer = New-Object System.Net.CookieContainer
    
    $postData = "User=UserName&Password=Pass"
    
    $buffer = [text.encoding]::ascii.getbytes($postData)
    
    [net.httpWebRequest] $req = [net.webRequest]::create($url)
    $req.method = "POST"
    $req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    $req.Headers.Add("Accept-Language: en-US")
    $req.Headers.Add("Accept-Encoding: gzip,deflate")
    $req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7")
    $req.AllowAutoRedirect = $false
    $req.ContentType = "application/x-www-form-urlencoded"
    $req.ContentLength = $buffer.length
    $req.TimeOut = 50000
    $req.KeepAlive = $true
    $req.Headers.Add("Keep-Alive: 300");
    $req.CookieContainer = $CookieContainer
    $reqst = $req.getRequestStream()
    $reqst.write($buffer, 0, $buffer.length)
    $reqst.flush()
    $reqst.close()
    [net.httpWebResponse] $res = $req.getResponse()
    $resst = $res.getResponseStream()
    $sr = new-object IO.StreamReader($resst)
    $result = $sr.ReadToEnd()
    $res.close()
    
    
    $web = new-object net.webclient
    $web.Headers.add("Cookie", $res.Headers["Set-Cookie"])
    $result = $web.DownloadString("https://secure_url")
    

提交回复
热议问题