使用POCO上传文件 - SSL连接意外关闭异常 [英] Upload a file using POCO - SSL Connection Unexpectedly Closed Exception

查看:1092
本文介绍了使用POCO上传文件 - SSL连接意外关闭异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用POCO将文件上传到HTTPS网址HTTP POST请求始终返回SSL连接意外关闭异常

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

以下是我用于Multipart上传的代码一个文件..

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<<"\nPath: "<<path;


  std::ifstream f1 (filePath,std::fstream::binary);
std::string content((std::istreambuf_iterator<char>(f1)), std::istreambuf_iterator<char>());

std::cout<<"\n Fle Content: "<<content;

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<<"\nReq Body: "<<reqBody.c_str();
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 <<"Upload Exception: "<< e.displayText() << std::endl;
}

我也试过Html表格:

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();

但两种方式都会返回相同的错误。
通过其他平台上传是有效的,所以我可以说它不是服务器问题,但问题出在上面的代码中。请帮我解决这个问题。
注意:服务器不支持分块传输。服务器端错误日志显示Bad transfer encoding:chunked。虽然我没有进行分块转移。

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.

更新:

我终于能够使用第一个代码上传文件(通过设置边界),但我正在讨论的异常(SSL连接意外关闭)即将到来,当我尝试从流中读取响应主体时使用:

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();

outStringStream << inStream.rdbuf();

服务器重新调整纯文本。我怎么能得到它?

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

推荐答案

我能够通过逐个字符地读取响应体来处理异常。以下是使用POCO上传文件的完整代码。

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.

http://developersarea.wordpress.com/2014/10/08/upload-files-using-poco-c-libraries/

这篇关于使用POCO上传文件 - SSL连接意外关闭异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆