pocoserver无限重启_使用POCO上传文件 - SSL连接意外关闭异常

Upload a file to a HTTPS url using POCO HTTP POST request always returns "SSL Connection Unexpectedly Closed" Exception

Below is the code i am using for Multipart upload of a file..

try

{

Poco::URI uri(uploadLink);

const Poco::Net::Context::Ptr context = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "", Poco::Net::Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");

Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(), context);

session.setKeepAlive(true);

// prepare path

std::string path(uri.getPathAndQuery());

if (path.empty())

{

path = "/";

}

std::cout<

std::ifstream f1 (filePath,std::fstream::binary);

std::string content((std::istreambuf_iterator(f1)), std::istreambuf_iterator());

std::cout<

std::string boundary = "-------------------------87142694621188";

std::string data1("---------------------------87142694621188\r\nContent-Disposition: form-data; name=\"data\"; filename=\"");

std::string data2(filePath);

std::string data3("\";\r\nContent-Type: application/octet-stream\r\n\r\n");

std::string data4("\r\n---------------------------87142694621188--\r\n");

std::string reqBody = data1 +data2 +data3 + content + data4;

std::cout<

Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPMessage::HTTP_1_1);

req.setKeepAlive(true);

req.setContentType("multipart/form-data; boundary=-------------------------87142694621188");

req.setContentLength(reqBody.length());

// sends request, returns open stream

std::ostream& myOStream = session.sendRequest(req);

// sends the body

myOStream << reqBody;

Poco::Net::HTTPResponse res;

// get the response body from server

std::istream& inStream = session.receiveResponse(res);

std::ostringstream outStringStream;

outStringStream << inStream.rdbuf();

std::cout<< outStringStream.str();

}

catch (Poco::Exception& e)

{

std::cout <

}

I also tried with Html forms:

Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPMessage::HTTP_1_1);

request.setKeepAlive(true);

request.setContentLength(1041);

Poco::Net::HTMLForm form;

form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);

form.addPart("file", new Poco::Net::FilePartSource(filePath));

form.prepareSubmit(request);

session.setTimeout(Poco::Timespan(20, 0));

form.write(session.sendRequest(request));

Poco::Net::HTTPResponse res;

std::istream &is = session.receiveResponse(res);

Poco::StreamCopier::copyStream(is, std::cout);

std::cerr << is.rdbuf();

But both ways returns the same error.

Upload through other platforms is working, so i can say its not the server issue, but the issue is in the above code. Please help me with the issue.

Note: Server does not support chunked transfer. And the server side error log says "Bad transfer encoding: chunked". Although i am not doing chunked transfer.

Update:

I am finally able to upload files using the first code(by setting boundary), But the exception i was talking about(SSL connection Unexpectedly Closed) is coming when i try to read the response body from stream using:

outStringStream << inStream.rdbuf();

Server is retuning a plain text. How can i get that?

解决方案

I am able to handle the exception by reading the response body character by character. Here is the complete code to upload a file using POCO.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以,以下是使用 Poco 库实现生产者-消费者模型的示例代码: ``` #include <iostream> #include <queue> #include <thread> #include <chrono> #include "Poco/Thread.h" #include "Poco/Mutex.h" #include "Poco/Condition.h" using namespace std; const int MAX_QUEUE_SIZE = 10; // 最大缓冲队列长度 queue<int> g_queue; // 全局缓冲队列 Poco::Mutex g_mutex; // 全局互斥量 Poco::Condition g_cond(g_mutex); // 全局条件变量 // 生产者线程函数 void producerThreadFunc() { for (int i = 1; i <= 20; i++) { Poco::Mutex::ScopedLock lock(g_mutex); // 加锁 while (g_queue.size() >= MAX_QUEUE_SIZE) { g_cond.wait(); // 队列已满,等待消费者消费 } g_queue.push(i); // 生产一个数据 cout << "生产者生产了数据:" << i << endl; g_cond.signal(); // 通知消费者 lock.unlock(); // 解锁 std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 等待一段时间 } } // 消费者线程函数 void consumerThreadFunc() { while (true) { Poco::Mutex::ScopedLock lock(g_mutex); // 加锁 while (g_queue.empty()) { g_cond.wait(); // 队列为空,等待生产者生产 } int data = g_queue.front(); // 取出一个数据 g_queue.pop(); // 从队列中删除该数据 cout << "消费者消费了数据:" << data << endl; g_cond.signal(); // 通知生产者 lock.unlock(); // 解锁 std::this_thread::sleep_for(std::chrono::milliseconds(500)); // 等待一段时间 } } int main() { // 创建生产者线程和消费者线程 std::thread producerThread(producerThreadFunc); std::thread consumerThread(consumerThreadFunc); // 等待两个线程结束 producerThread.join(); consumerThread.join(); return 0; } ``` 在上面的代码中,我们使用 Poco 库中的 Mutex 类和 Condition 类来实现互斥和条件变量,从而实现了生产者-消费者模型。其中,Mutex 类用于实现互斥锁,Condition 类用于实现条件变量,生产者和消费者线程通过互斥锁和条件变量来同步访问全局缓冲队列。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值