我正在使用Poco編寫C++中的HTTP客戶端,並且存在服務器使用jpeg圖像內容(以字節爲單位)發送響應的情況。我需要客戶端來處理響應並從這些字節生成一個jpg圖像文件。如何使用Poco C++從HTTP服務器響應中讀取圖像內容?
我搜索了Poco圖書館的適當功能,但我還沒有找到任何。看來唯一的辦法就是手動。
這是我的代碼的一部分。它接受響應並使輸入流在圖像內容的開始處開始。
/* Get response */
HTTPResponse res;
cout << res.getStatus() << " " << res.getReason() << endl;
istream &is = session.receiveResponse(res);
/* Download the image from the server */
char *s = NULL;
int length;
std::string slength;
for (;;) {
is.getline(s, '\n');
string line(s);
if (line.find("Content-Length:") < 0)
continue;
slength = line.substr(15);
slength = trim(slength);
stringstream(slength) >> length;
break;
}
/* Make `is` point to the beginning of the image content */
is.getline(s, '\n');
如何繼續?
我不推倒重來,我使用波科。 – 2012-01-14 16:03:49
Leif,如果Poco包含一個HTTP庫,並且您將使用它,那麼您將不必解析Content-Length。 – 2012-01-15 10:32:17
如果你知道用Poco查找Content-Length而不用手動解析的方法,那你爲什麼不告訴我它是什麼? – 2012-01-17 22:29:43