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

2016-02-19 21:05 118 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 

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

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

延伸阅读
标签: Web开发
加上设置字符编码的方法: response.setHeader("charset","gb2312"); ******************************************** 看到的说明原文如下: 用AJAX来GET回一个页面时,RESPONSETEXT里面的中文多半会出现乱码,这是因为xmlhttp在处理返回的responseText的时候,是把resposeBody按UTF-8编码进解码考形成的,如果服务器送出...
标签: Web开发
使用库元素必须首先在DW中正确建立站点。 库被设计用来使重复性的工作更快、更容易并尽可能地无差错。 任何网页中的元素,无论文本还是图形均可以指定为库元素。 库元素可以用来放置在同一个站点内的任何页面中,而不需要重新输入文本或插入图片等。 可以在任何时候修改库文件。编辑完成,保存,DW会同时更新所有应用...
标签: Excel 电脑
打开Excel 在excel中打开需要使用if函数的表格。如示例中需要给学生按成绩判定该学生是否及格。 输入if函数 鼠标移至想要判定的单元格后面的单元格里,输入“=if()”。 输入数值 鼠标移至“=if()”的括号当中,我们可以看到引伸出一列对if函数英文解释,其中logical-test表明该位置输入if函数判断条件,value-if-...
底纹填充是随机生成的填充,可用来赋予对象自然的外观。CorelDRAW提供预设的底纹,而且每种底纹均有一组可以更改的选项。可以使用任一颜色模型或调色板中的颜色来自定义底纹填充。底纹填充只能包含RGB颜色,但是,可以使用其它颜色模型和调色板作为参考来选择颜色。底纹填充功能强大,可以增强图形的效果。但是,会增加文件大小以及延长打印时...
Progress控件能让人们感受到一个应用程序执行的进度,在很多应用程序中都能用到它,但通常只支持在单任务中,在Windows98/NT中文操作系统下,在VC++6.0环境下,利用线程编制了一个非常小巧的应用程序来实现Progress控件的使用。它可以支持多线程,使用起来很方便。以下是这个应用程序的源代码: //ProgressDialog.h class CProgressD...

经验教程

662

收藏

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