none
What do $request.GetResponse() do? RRS feed

  • Question

  • Referring to following codes,

    Case 1 : with following coding, I get error "The remote server returned an error: (4050 Method Not Allowed."

    Case 2 : when I remove following codes, file can be downloaded, and I would like to know on what following codes do, would it be necessary for download process?

    $response = $request.GetResponse()
    $response.Close()

    Does anyone have any suggestions?
    Thanks in advance for any suggestions

    $clnt = new-object System.Net.WebClient
    [Net.ServicePointManager]::SecurityProtocol = 'Tls12'
    $url = "https://rbwm-api.hsbc.com.hk/digital-pws-tools-mpf-eapi-prod-proxy/v1/mpf/unit-prices/download/HSBCTRUSTPLUS?fund=APF&startDate=" + (Get-Content 'D:\Documents\TS\txt\MPFSY.txt') + "&endDate=" + (Get-Content 'D:\Documents\TS\txt\MPFEY.txt') + "&locale=zh_HK"    
    $file = "D:\Folder\ABC.xls"
    $request = [System.Net.WebRequest]::Create($url) $request.Method = 'HEAD' #$response = $request.GetResponse() $httpStatus = $response.StatusCode $urlIsValid = ($httpStatus -eq 'OK') $tryError = $null #$response.Close() $clnt.DownloadFile($url,$file) # leave the loop if successfull



    Thanks in advance for any suggestions





    Thursday, July 25, 2019 2:28 AM

Answers

  • To download a file use "DownloadFile()".  GetResponse is for returning web pages.

    Be sure your url is constructed correctly.  Be sure your files contain only one line. If they don't then you will get a bad URL.

    $wc = new-object System.Net.WebClient
    [Net.ServicePointManager]::SecurityProtocol = 'Tls12'
    $startDate = Get-Content D:\Documents\TS\txt\MPFSY.txt -Raw
    $endDate = Get-Content D:\Documents\TS\txt\MPFEY.txt -Raw
    $url = 'https://rbwm-api.hsbc.com.hk/digital-pws-tools-mpf-eapi-prod-proxy/v1/mpf/unit-prices/download/HSBCTRUSTPLUS?fund=APF&startDate=$startDate&endDate=$endDate+&locale=zh_HK'
    $file = 'D:\Folder\ABC.xls'
    $wc.DownloadFile($url, $file)
    


    \_(ツ)_/

    • Marked as answer by oemMicrosoft Thursday, July 25, 2019 3:36 AM
    Thursday, July 25, 2019 3:12 AM

All replies

  • To download a file use "DownloadFile()".  GetResponse is for returning web pages.

    Be sure your url is constructed correctly.  Be sure your files contain only one line. If they don't then you will get a bad URL.

    $wc = new-object System.Net.WebClient
    [Net.ServicePointManager]::SecurityProtocol = 'Tls12'
    $startDate = Get-Content D:\Documents\TS\txt\MPFSY.txt -Raw
    $endDate = Get-Content D:\Documents\TS\txt\MPFEY.txt -Raw
    $url = 'https://rbwm-api.hsbc.com.hk/digital-pws-tools-mpf-eapi-prod-proxy/v1/mpf/unit-prices/download/HSBCTRUSTPLUS?fund=APF&startDate=$startDate&endDate=$endDate+&locale=zh_HK'
    $file = 'D:\Folder\ABC.xls'
    $wc.DownloadFile($url, $file)
    


    \_(ツ)_/

    • Marked as answer by oemMicrosoft Thursday, July 25, 2019 3:36 AM
    Thursday, July 25, 2019 3:12 AM
  • Thank you very much for suggestions (^v^)

    Thanks in advance for any suggestions

    Thursday, July 25, 2019 3:37 AM
  • have you looked at Invoke-WebRequest and Invoke-RestMethod?

    I use Invoke-WebRequest all the time on my windows servers so i don't have to deal with the IE Enhanced Security frustration.  

    For example:  Invoke-WebRequest - URI "https://some.remote.server/some/path/tofile.ext" -OutFile c:\temp\tofile.ext

    You even get a progress bar in PowerShell with this (when the remote file size is accessible).


    -Eriq VanBibber, CTO, Priasoft Inc.

    Thursday, July 25, 2019 8:01 PM
  • have you looked at Invoke-WebRequest and Invoke-RestMethod?

    I use Invoke-WebRequest all the time on my windows servers so i don't have to deal with the IE Enhanced Security frustration.  

    For example:  Invoke-WebRequest - URI "https://some.remote.server/some/path/tofile.ext" -OutFile c:\temp\tofile.ext

    You even get a progress bar in PowerShell with this (when the remote file size is accessible).


    -Eriq VanBibber, CTO, Priasoft Inc.

    The WebClient is the same as Invoke-WebRequest. Both call the same underlying APIs. The OP is not using IE. IE does not need to be installed for either to work. Both are restricted by the same security boundary as IE and all browsers that run on Windows. All network requests are secured the same way.

    IE and Edge both add parsing restrictions but this does not affect the Net web classes.  Neither web call allows scripting so most restrictions are no required as they are in a browser.

    WebClient.DownloadFile() is more convenient and can be faster.


    \_(ツ)_/

    Thursday, July 25, 2019 8:07 PM
  • Good clarifications.

    I wasn't trying to imply that IE was required...but i can see how that could be misconstrued.

    And true, i knew about the underlying .NET classes...but when others read this post, and if this wasn't mentioned, it could cause other to think the .NET classes are the only way.



    -Eriq VanBibber, CTO, Priasoft Inc.

    Thursday, July 25, 2019 8:10 PM
  • Yes. The CmdLet simplifies access to [System.Net.WebRequest] but the WebClient "DownloadFile()" does not have to convert the stream to a file. It just streams the file from the site. It also displays a progress bar if you set one up. This class is more reliable for large files as it runs async, if needed, and can also handle disruptions. It is a good generalization of file and block operations for both uploading and downloading file.

    It is idiotic to try and download files with IE as you will always get a challenge.  Most modern web sites will not allow page scraping to get files.


    \_(ツ)_/

    Thursday, July 25, 2019 8:20 PM