问题
I'm trying to convert the full string of bytes from a file opened in binary mode to a string encoded in the ISO-8859-1 character set. My understanding is that by converting to ISO-8859-1 all binary information from the file is retained which is why it is being converted to that format. Is this a valid statement?
I'm working in C++ (Visual Studio 2017) building an executable to be used on the Windows platform. I'm not experienced in HTTP programming.
I have prototype code written in PowerShell code which successfully performs the functionality that I'm trying to duplicate in C++. In the power shell code an HTTP message is sent to upload a firmware file to a device.
- The message includes headers and a body:
- The message is using the multipart/form-data protocol.
An example of the headers of the message are:
Authorization: Bearer Yosda0IDRQuVaU_L0SnV5g== Content-Type: multipart/form-data; boundary=42b745c8-4da8-454e-8c13-cbb5c1f7694f Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
An example of the body of the message is:
--1a2fc07a-d882-4470-a1da-79716d34cd9b Content-Disposition: form-data; name="upgrade_file"; filename="" Content-Type: application/octet-stream // File data (encoded in ISO-8859-1 format) goes here // --1a2fc07a-d882-4470-a1da-79716d34cd9b Content-Disposition: form-data; name="submit" Install OS --1a2fc07a-d882-4470-a1da-79716d34cd9b--
The PowerShell script is converting the file with the following lines of code:
$bytes = [System.IO.File]::ReadAllBytes($file.FullName);
if ( $bytes )
{
$enc = [System.Text.Encoding]::GetEncoding(iso-8859-1);
$data = $enc.GetString($bytes);
}
Once the headers and body are setup the message is sent and the firmware is uploaded when running the PowerShell code.
In my C++ code I've got the headers and the pre-body and post-body codes worked out. I think (not sure) that I just need to convert the file to iso-8859-1 to get the message to work now.
I'm using libcurl to send the message.
res = curl_easy_perform(pCurl);
Currently (without converting file to iso-8859-1) I get the following error message from the function call:
Failure when receiving data from the peer
When the message is sent I can see that only some bytes are uploaded. I assume that could be because the file data is not properly encoded and when it is reading that data it reaches some point where the data is in a format that it cannot handle.
来源:https://stackoverflow.com/questions/55385298/how-do-i-encode-binary-file-data-to-iso-8859-1-to-attach-to-the-body-of-an-http