Use Visual C ++ to publish Word files under SSL

Source: Internet
Author: User

The background management of a website project requires a function to be implemented. The document publishing function is implemented by uploading Word files. The process is as follows: When you select a Word document, the system automatically identifies the title part and converts the remaining part to HTML for publishing as the body.

The original design idea was to upload the word to the background and parse the document in Java. After some twists and turns, I found that Java's support for word is not ideal, and there is a garbled problem. I decided to use ActiveX controls on the client. The control parses Word documents and automatically uploads them.

It is difficult to parse Word documents. You can only support Word documents of a certain version through # import or wizard. The versions of word have gone through word2000, wordxp, word2002, word2003, and the current word2007 respectively since word97. Based on a large number of implementation solutions, we found that Microsoft's autowrap solution is the most ideal: http://support.microsoft.com/kb/216686/zh-cn. This solution encapsulates the com idispatch interface and calls the com attributes and methods by simulating a script call. In addition, it can implement the default parameter call to solve this kind of problem.

In this way, the word can be converted to HTML through the saveas method of _ document. The next question is how to package and upload. The first question is how to package, naturally think of zip. Because MFC does not support zip, the official zlib library cannot implement folder compression. A previous project can only be replaced by RAR because the solution cannot be found. The disadvantage is that RAR executable files must be included. Finally, find a very useful C ++ source code on codeproject, zip utils: http://www.codeproject.com/kb/files/zip_utils.aspx. The usage is very simple. You only need to include a zip. h file. To create a zip file, follow these steps:

Hzip = createzip (m_pszzippath, null); <br/> zipadd (hzip, stempfilepath, stempfilename); <br/> ...... <Br/> closezip (hzip );

The last step is to upload the file. This is easy to handle. There are a lot of code for using HTTP to upload files on the Internet. I can find some useful code and compile it. I wrote a script in PHP and the test was normal.

An exciting release. There was a problem during the test. He used the fileupload class under Tomcat and encountered an exception. No one provided a solution for this exception on the Internet. I cannot explain it, but HTTP is hidden in the calm water. What is the problem? I only need to analyze the HTTP header.

Install fiddler2, write a servlet class, and start testing. For comparison, an upload form is written using the HTML form, the same file is uploaded, and then compared with the HTTP header uploaded by the control. After half a day of comparison, we found that the two are basically the same except for agent and other information. Desperate, a line of staring at ultraedit suddenly found that the HTTP header generated by IE had a line break between two fields. With a skeptical attitude, I tried to add a line break ...... Click Run. I found the problem, but it was too embarrassing to delete the/R/N in the code. I thought it was redundant. In the past, this was an agreement, and it was not a bit false.

Therefore, packaging, publishing, and customer testing are all normal, and the code is also discarded. Suddenly one day, when discussing the security of the system, I suddenly thought that the customer's background was logged on through htts + USB key, but the file was published as HTTP, which was a little serious.

I quickly found the source code and started to modify it, but found that all of a sudden I was confused, and I was almost confused about SSL. According to msdn, to implement HTTPS transmission, you only need to input the internet_flag_secure parameter in the openrequest of MFC. I thought it was so simple. I tried to test the code quickly, but found that an exception was directly returned after sendrequest was called: the client was not authorized. Go to msdn again to find out the cause and write the following code to solve the problem:

Cstring strserver, strobject; </P> <p> DWORD dwservicetype, dwflags; </P> <p> internet_port nport; </P> <p> afxparseurl (serverpath, dwservicetype, strserver, strobject, nport); </P> <p> dwflags = dwservicetype = afx_inet_service_https? Internet_flag_secure: 0; </P> <p> phttpconnection = session. gethttpconnection (strserver, nport); </P> <p> phttp = phttpconnection-> openrequest (chttpconnection: http_verb_post, strobject, <br/> null, session. getcontext (), null, null, dwflags ); </P> <p> // send the header request </P> <p> phttp-> addrequestheaders (makerequestheaders (strhttpboundary )); </P> <p> bsuccess = false; <br/> while (! Bsuccess) <br/>{< br/> try <br/>{< br/> bsuccess = phttp-> sendrequestex (dwtotalrequestlength, <br/> hsr_sync | hsr_initiate ); <br/>}< br/> catch (cinternetexception * E) <br/>{< br/> DWORD dwerror = e-> m_dwerror; </P> <p> switch (dwerror) <br/> {<br/> case error_internet_client_auth_cert_needed: <br/> case when: <br/> case error_internet_incorrect_password: <br/> Ca Se error_internet_invalid_ca: <br/> case error_internet_post_is_non_secure: <br/> case when: <br/> case error_internet_sec_cert_date_invalid: <br/> // return error_success regardless of clicking on OK or cancel <br/> If (phttp-> errordlg (cwnd: fromhandle (getjavastopwindow ()), <br/> dwerror, <br/> flags_error_ui_filter_for_errors <br/> | flags_error_ui_flags_generate_dat A <br/> | flags_error_ui_flags_change_options, <br/> null )! = Error_success) <br/>{< br/> throw E; <br/>}< br/> break; </P> <p> default: <br/> throw E; <br/>}</P> <p> E-> Delete (); <br/>}< br/>

The problem is solved successfully.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.