'How to verify New-HttpWebRequest status code
I found this code in stackoverflow to upload file to Nexus repository but I'm not sure how to verify if the httprequest was completed successfully.
function Upload-File
{
<#
.SYNOPSIS
Uploads a file to the Nexus repository.
.DESCRIPTION
Uploads a file to the Nexus repository.
If the file was uploaded successfully, the url via which the resource can be downloaded is returned.
.PARAMETER Url
The Url where the resource should be created.
Please note that underscores and dots should be encoded, otherwise the Nexus repository does not accept the upload.
.PARAMETER File
The file that should be uploaded.
.PARAMETER Credential
Credential used for authentication at the Nexus repository.
.EXAMPLE
Upload-File -Url https://nexusrepo.domain.com/repository/repo-name/myfolder/myfile%2Eexe -File (Get-ChildItem .\myfile.exe) -Credential (Get-Credential)
.OUTPUTS
If the file was uploaded successfully, the url via which the resource can be downloaded.
#>
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$Url,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]$File,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[pscredential]$Credential
)
Write-Verbose "Upload-File Url:$Url"
Configure-Tls -Url $Url;
$fileSizeBytes = $File.Length;
#$bufSize = Get-BufferSize $fileSizeBytes;
$bufSize = 10 * 1MB;
Write-Verbose ("FileSize is {0} bytes ({1:N0}MB). BufferSize is {2} bytes ({3:N0}MB)" -f $fileSizeBytes,($fileSizeBytes/1MB),$bufSize,($bufSize/1MB)) -Verbose
if (Get-UseChunkedUpload -FileSize $fileSizeBytes -BufferSize $bufSize)
{
Write-Verbose "Using chunked upload. Send pre-auth first." -Verbose
Send-PreAuthenticate -Url $Url -Credential $Credential;
}
$progressActivityMessage = ("Sending file {0} - {1} bytes" -f $File.Name, $File.Length);
$webRequest = New-HttpWebRequest -Url $Url -Credential $Credential -FileSize $fileSizeBytes -BufferSize $bufSize;
$chunk = New-Object byte[] $bufSize;
$bytesWritten = 0;
$fileStream = [System.IO.File]::OpenRead($File.FullName);
$requestStream = $WebRequest.GetRequestStream();
try
{
while($bytesRead = $fileStream.Read($chunk,0,$bufSize))
{
$requestStream.Write($chunk, 0, $bytesRead);
$requestStream.Flush();
$bytesWritten += $bytesRead;
$progressStatusMessage = ("Sent {0} bytes - {1:N0}MB" -f $bytesWritten, ($bytesWritten / 1MB));
#Write-Progress -Activity $progressActivityMessage -Status $progressStatusMessage -PercentComplete ($bytesWritten/$fileSizeBytes*100);
}
}
catch
{
throw;
}
finally
{
if ($fileStream)
{
$fileStream.Close();
}
if ($requestStream)
{
$requestStream.Close();
$requestStream.Dispose();
$requestStream = $null;
}
}
}
how could I check if this was completed successfully? meaning the file was uploaded successfully. Please advise. I've trying to find a way to upload large file to Nexus repo for long time and this seems to be the only working solution but I am not sure how to check if it's successful.
Thank you for your help in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
