How to Simulate A Form Post Request By Using Wininet

xiaoxiao2021-03-06  39

This Article Was previously Published Under Q165298

On this Page

Summarymore informationReferences

Summary

TO Properly Simulate A Formission 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-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 Hexadecimal 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");

Static LPSTR Accept [2] = {"* / *", null};

// 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 About URL-Encoding and the Format of A Form Post Request, See Section 8.2 in RFC 1866.

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

New Post(0)