在VC中WININET如何使用HTTP的POST方法

2016-02-19 21:05 124 1 收藏

下面是个超简单的在VC中WININET如何使用HTTP的POST方法教程,图老师小编精心挑选推荐,大家行行好,多给几个赞吧,小编吐血跪求~

【 tulaoshi.com - 编程语言 】

SUMMARY
    To properly simulate a Form submission using WinInet, you need to send a header that indicates the proper Content-Type. For Forms, the proper Content-Type header is: Content-Type: application/x-www-form-urlencoded
    
    MORE INFORMATION
    In many cases, the server does not respond appropriately if a Content-Type is not specified. For example, the Active Server Pages component of IIS 3.0 actually checks this header specifically for 'application/x-www-form- urlencoded' before adding form variables to the "Request.Form" object. This MIME/Content-Type indicates that the data of the request is a list of URL- encoded form variables. URL-encoding means that space character (ASCII 32) is encoded as '+', special character such '!' encoded in hexadecemal form as '%21'.
    
    Here is a snippet of code that uses the MFC WinInet classes to simulate a Form POST request:
     CString strHeaders =
     _T("Content-Type: application/x-www-form-urlencoded");
     // URL-encoded form variables -
     // name = "John Doe", userid = "hithere", other = "P&Q"
     CString strFormData = _T("name=John+Doe&userid=hithere&other=P%26Q");
    
     CInternetSession session;
     CHttpConnection* pConnection =
     session.GetHttpConnection(_T("ServerNameHere"));
     CHttpFile* pFile =
     pConnection-OpenRequest(CHttpConnection::HTTP_VERB_POST,
     _T("FormActionHere"));
     BOOL result = pFile-SendRequest(strHeaders,
     (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
    
    
    Without MFC, the same code translates to straight SDK calls as follows:
     static TCHAR hdrs[] =
     _T("Content-Type: application/x-www-form-urlencoded");
     static TCHAR frmdata[] =
     _T("name=John+Doe&userid=hithere&other=P%26Q");
     statuc TCHAR accept[] =
     _T("Accept: */*");
    
     // for clarity, error-checking has been removed
     HINTERNET hSession = InternetOpen("MyAgent",
     INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     HINTERNET hConnect = InternetConnect(hSession, _T("ServerNameHere"),
     INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",
     _T("FormActionHere"), NULL, NULL, accept, 0, 1);
     HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
     // close any valid internet-handles 

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

来源:http://www.tulaoshi.com/n/20160219/1625238.html

延伸阅读
标签: Web开发
使用库元素必须首先在DW中正确建立站点。 库被设计用来使重复性的工作更快、更容易并尽可能地无差错。 任何网页中的元素,无论文本还是图形均可以指定为库元素。 库元素可以用来放置在同一个站点内的任何页面中,而不需要重新输入文本或插入图片等。 可以在任何时候修改库文件。编辑完成,保存,DW会同时更新所有应用...
首先,你必须手工将WAV文件加入到资源文件.rc 中。 象这样:wave 例子:cool wave c:projectssoundscool.wav 然后,在你需要播放WAV的地方加入下面的函数: bool playresource(lpstr lpname) { bool brtn; lpstr lpres; handle hres; hrsrc hresinfo; hinstance nl=afxgetinstancehandle...
底纹填充是随机生成的填充,可用来赋予对象自然的外观。CorelDRAW提供预设的底纹,而且每种底纹均有一组可以更改的选项。可以使用任一颜色模型或调色板中的颜色来自定义底纹填充。底纹填充只能包含RGB颜色,但是,可以使用其它颜色模型和调色板作为参考来选择颜色。底纹填充功能强大,可以增强图形的效果。但是,会增加文件大小以及延长打印时...
Progress控件能让人们感受到一个应用程序执行的进度,在很多应用程序中都能用到它,但通常只支持在单任务中,在Windows98/NT中文操作系统下,在VC++6.0环境下,利用线程编制了一个非常小巧的应用程序来实现Progress控件的使用。它可以支持多线程,使用起来很方便。以下是这个应用程序的源代码: //ProgressDialog.h class CProgressD...
标签: Web开发
ajax.html !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://www.w3.org/1999/xhtml" head meta http-equiv="Content-Type" content="text/html; charset=gb2312" / title兼容多浏览器的AJAX入门实例(超详细注释)/title script type="text/ja...

经验教程

365

收藏

93
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部