InetHttpSendForm example for C++ 5.0 and later
Take the following steps to use HTTP features to your application:
1. |
Start with a copy of the project that you created in Loading and Displaying an Image |
|
Note: This example changes the initialization of the window; so using a copy of the original project prevents problems with later reuse of the original project. |
2. |
Add #import and #include statements to your program so you can access the LEAD COM constants and classes: |
|
|
a. |
In the Project Workspace, click the FileView tab. |
|
b. |
Double-click the tutor files folder to open it. |
|
c. |
Double-click the Header Files folder to open it. |
|
d. |
Double-click the StdAfx.h file to edit it. |
|
e. |
Add the following line to the end of the file (keep in mind, you may have to change the path to where the DLL resides): |
#import "c:\\winnt\\system32\\LTRVR14N.dll"
no_namespace, named_guids
#import "c:\\winnt\\system32\\LTRHT14N.dll" no_namespace, named_guids
3. |
Edit the TUTORDLG.H file and change the definition of CTutorDlg : CDialog by inserting the following lines after DECLARE_MESSAGE_MAP: |
public:
ILEADRasterHTTP *m_pRasterHTTP;
4. |
Edit the TutorDlg.cpp file and add the following code to the end of the OnInitDialog function (just before return TRUE): |
//Create the RasterHTTP object
CoCreateInstance(CLSID_LEADRasterHTTP, NULL, CLSCTX_ALL, IID_ILEADRasterHTTP,
(void**)&m_pRasterHTTP);
5. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: |
|
|
a. |
In the Class name combo box, select CTutorDlg. |
|
b. |
In the Object IDs list box, select CTutorDlg. |
|
c. |
In the Messages list box, select WM_DESTROY. |
|
d. |
Click the Add function button. Choose OK for the default function name (OnDestroy). |
|
e. |
Click the Edit Code button to start entering the code. |
6. |
Enter new code as follows (just before the CDialog::OnDestroy call): |
void CTutorDlg::OnDestroy()
{
m_pRasterHTTP->Release();
CDialog::OnDestroy();
}
7. |
Add a command buttons to the Tutor dialog and name it as follows: |
||
|
ID |
Caption | |
|
IDC_HTTP_FORM |
HTTP Form | |
8. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: |
||
|
a. |
Click the Message Maps tab. |
|
|
b. |
In the Class Name combo box, select CTutorDlg. |
|
|
c. |
In the Object IDs list box, select IDC_HTTP_FORM. |
|
|
d. |
In the Messages list box, select BN_CLICKED. |
|
|
e. |
Click the Add function button. Choose OK for the default function name (OnHttpForm). |
|
|
f. |
Click the Edit Code button and enter the following code: (you will have to change the IP address to a valid server name or address) |
void CTutorDlg::OnHttpForm()
{
short nRet;
long nStatus;
nRet = m_pRasterHTTP->InetHttpConnect("demo.leadtools.com",
80, "", "");
if(nRet == 0)
{
nRet = m_pRasterHTTP->InetHttpOpenRequest(HTTP_RQST_POST,
"/com/tutorial/http/form.asp", "", "HTTP/1.0");
if(nRet == 0)
{
m_pRasterHTTP->InetHttpFormValueCount =
2;
m_pRasterHTTP->InetHttpFormValues[0]->pszName
= "email";
m_pRasterHTTP->InetHttpFormValues[0]->pszValue
= TEXT("myemail@mydomain.com");
m_pRasterHTTP->InetHttpFormValues[1]->pszName
= TEXT("name");
m_pRasterHTTP->InetHttpFormValues[1]->pszValue
= TEXT("First Last");
nRet = m_pRasterHTTP->InetHttpSendForm();
if(nRet != 0)
{
AfxMessageBox(TEXT("Error
Sending Form"));
m_pRasterHTTP->InetHttpCloseRequest();
m_pRasterHTTP->InetHttpDisconnect();
return;
}
m_pRasterHTTP->InetHttpGetServerStatus(&nStatus);
if(nStatus == HTTP_STATUS_OK)
{
FILE
* fp = _topen(TEXT("c:\\output.htm"), TEXT("wb"));
if(fp)
{
_bstr_t
szResponse = m_pRasterHTTP->InetHttpGetResponse();
fwrite((TCHAR
*)szResponse, szResponse.length(), 1, fp);
fclose(fp);
}
}
else
{
AfxMessageBox(TEXT("Problem
With Server"));
}
m_pRasterHTTP->InetHttpCloseRequest();
m_pRasterHTTP->InetHttpDisconnect();
}
}
}
9. |
Rebuild and run the application. |