我正在嘗試使用用 C (Windows) 撰寫的客戶端程式將二進制檔案發布到 Web 服務器。我對套接字編程還很陌生,因此嘗試使用multipart/form-data純文本訊息和基于文本的檔案(.txt、.html、.xml)的POST 請求。這些似乎作業正常。但是在嘗試發送 PNG 檔案時,我遇到了一些問題。
以下是我讀取二進制檔案的方式
FILE *file;
char *fileName = "download.png";
long int fileLength;
//Open file, get its size
file = fopen(fileName, "rb");
fseek(file, 0, SEEK_END);
fileLength = ftell(file);
rewind(file);
//Allocate buffer and read the file
void *fileData = malloc(fileLength);
memset(fileData, 0, fileLength);
int n = fread(fileData, 1, fileLength, file);
fclose(file);
我確認所有位元組都被正確讀取。
這就是我形成郵件標題和正文的方式
//Prepare message body and header
message_body = malloc((int)1000);
sprintf(message_body, "--myboundary\r\n"
"Content-Type: application/octet-stream\r\n"
"Content-Disposition: form-data; name=\"myFile\"; filename=\"%s\"\r\n\r\n"
"%s\r\n--myboundary--", fileName, fileData);
printf("\nSize of message_body is %d and message_body is \n%s\n", strlen(message_body), message_body);
message_header = malloc((int)1024);
sprintf(message_header, "POST %s HTTP/1.1\r\n"
"Host: %s\r\n"
"Content-Type: multipart/form-data; boundary=myboundary\r\n"
"Content-Length: %d\r\n\r\n", path, host, strlen(message_body));
printf("Size of message_header is %d and message_header is \n%s\n", strlen(message_header), message_header);
當請求被正確接收時,連接和發送部分也能正常作業。但是,接收到的 png 檔案格式錯誤。fileData如果我%s在 printf 中使用,終端會列印出以下內容
?PNG
我四處搜索,發現二進制資料的行為不像字串,因此不能在它們上使用 printf/sprintf/strcat 等。由于二進制檔案嵌入了空字符,%s因此無法正確列印。看起來這就是fileData只列印 PNG 標題的原因。
目前,我向send()服務器發送了兩個請求。一個帶有頁眉,另一個帶有正文和頁腳。這適用于基于文本的檔案。為了避免使用sprintf二進制資料,我嘗試發送一個請求頭,一個請求二進制資料(正文),一個請求頁腳。這似乎也行不通。
此外,發現memcpy可用于將二進制資料附加到普通字串。那也沒有用。這是我嘗試的方法(不確定我的實作是否正確)。
sprintf(message_body, "--myboundary\r\n"
"Content-Disposition: form-data; name=\"text1\"\r\n\r\n"
"text default\r\n"
"--myboundary\r\n"
"Content-Type: application/octet-stream\r\n"
"Content-Disposition: form-data; name=\"myFile\"; filename=\"%s\"\r\n\r\n", fileName);
char *message_footer = "\r\n--myboundary--";
char *message = (char *)malloc(strlen(message_body) strlen(message_footer) fileLength);
strcat(message, message_body);
memcpy(message, fileData, fileLength);
memcpy(message, message_footer, strlen(message_footer));
我被困在如何發送需要附加字串(標題)、二進制資料(有效負載)、字串(頁腳)的有效負載上。
發送整個檔案的任何建議/指標/參考鏈接將不勝感激。謝謝!
uj5u.com熱心網友回復:
如何列印二進制資料
在你的問題中,你說你在列印二進制資料時遇到了麻煩printf,因為二進制資料包含值為 的位元組0。另一個問題(您沒有提到)是二進制資料可能包含不可列印的字符。
二進制資料通常以下列方式之一表示:
- 以十六進制表示
- 在文本表示中,用占位符替換不可列印的字符
- 以上兩者
我建議您創建自己的簡單函式來列印二進制資料,它實作了選項 #3。您可以使用該函式isprint來確定某個字符是否可列印,如果不可列印,您可以放置??一些占位符(例如'X')來代替。
這是一個執行此操作的小程式:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void print_binary( char *data, size_t length )
{
for ( size_t i = 0; i < length; i = 16 )
{
int bytes_in_line = length - i >= 16 ? 16 : length - i;
//print line in hexadecimal representation
for ( int j = 0; j < 16; j )
{
if ( j < bytes_in_line )
printf( "X ", data[i j] );
else
printf( " " );
}
//add spacing between hexadecimal and textual representation
printf( " " );
//print line in textual representation
for ( int j = 0; j < 16; j )
{
if ( j < bytes_in_line )
{
if ( isprint( (unsigned char)data[i j] ) )
putchar( data[i j] );
else
putchar( 'X' );
}
else
{
putchar( ' ' );
}
}
putchar( '\n' );
}
}
int main( void )
{
char *text = "This is a string with the unprintable backspace character \b.";
print_binary( text, strlen( text ) );
return 0;
}
該程式的輸出如下:
54 68 69 73 20 69 73 20 61 20 73 74 72 69 6E 67 This is a string
20 77 69 74 68 20 74 68 65 20 75 6E 70 72 69 6E with the unprin
74 61 62 6C 65 20 62 61 63 6B 73 70 61 63 65 20 table backspace
63 68 61 72 61 63 74 65 72 20 08 2E character X.
如您所見,該函式print_binary以十六進制表示和文本表示形式列印資料,每行 16 個位元組,并且'X'在列印文本表示形式時正確地用占位符替換了不可列印的退格字符。
錯誤的printf轉換格式說明符
線
printf("\nSize of message_body is %d and message_body is \n%s\n", strlen(message_body), message_body);
是錯的。的回傳型別strlen是size_t,不是int。的正確printf轉換格式說明符size_t是%zu,不是%d。使用錯誤的格式說明符會導致未定義的行為,這意味著它可能適用于某些平臺,但不適用于其他平臺。
用二進制資料連接字串
以下幾行是錯誤的:
char *message = (char *)malloc(strlen(message_body) strlen(message_footer) fileLength);
strcat(message, message_body);
memcpy(message, fileData, fileLength);
memcpy(message, message_footer, strlen(message_footer));
該函式strcat需要兩個函式引數都指向以空字符結尾的字串。但是,不能保證第一個函式引數以空字符結尾。我建議你使用strcpy而不是strcat.
此外,在您的問題中,您正確地指出應該將檔案二進制資料附加到字串中。然而,那不是什么行
memcpy(message, fileData, fileLength);
是在做。相反,它會覆寫字串。
為了將二進制資料附加到字串,您應該只覆寫字串的終止空字符,例如:
memcpy( message strlen(message), fileData, fileLength );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/393322.html
上一篇:小圓圈的交集?
