Simulate a form post request using wininet

zhaozj2021-02-17  51

This is an article in MSDN, saying that Wininet is used to simulate the sending of forms, using the VC and SDK methods implementation, and the method can be directly sent to the web server in writing, and directly Fill in the web on the webpage. The original text is short and very simple, there is no translation, directly on the post, did not understand the repost in MSDN or the collection, anyway, the friend can use the HOWTO: SIMULATE A FORM Post Request Using Wininetlast REVIED: February 10, 1998Article ID: Q165298 The Information in this article applies to:

The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C , 32-bit Editions, versions 4.2, 5.0 Microsoft Internet Client SDK, versions 4.0, 4.01 SUMMARY To properly simulate a Form submission using WinInet, you need to send a header that INDICES The Proper Content-Type. for Forms, The Proper Content-Type Header IS: Content-Type: Application / X-WWWW-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 HexadeMal Form as'% 21'. Here Is A Snippet of Code That 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.getttpConnection (_T ("ServerNameHere");

Chttpfile * pfile =

PConnection-> OpenRequest (ChttpConnection :: http_verb_post,

_T ("formactionhere"));

Bool result = pfile-> sendrequest (strheaders,

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

References for more information on url-encoding and the format of a form Post Request, See Section 8.2 in RFC 1866. Keywords: Axsdkwininet MFCMISC KBPRG

TECHNOLOGY: KBMFC

Version: Winnt: 1.0 4.2 5.0

Platform: NT Windows

Issue Type: Kbhowto

转载请注明原文地址:https://www.9cbs.com/read-30251.html

New Post(0)