Using the Navigate2 method of IWebBrowser2 interface to implement HTTP POST transmission

Source: Internet
Author: User
Tags http post int size valid
Using the Navigate2 method of IWebBrowser2 interface to implement Http POST transmission

Floating clouds 2007/11/8

In the development of the community game, the original Mini browser is required to expand to be able to open the Web page via HTTP POST transmission, for example: By submitting user Id,password, the user's action (view profile, view diary, open blog, etc.), To open the corresponding Web page in the Mini browser.

The implementation is achieved by acquiring the browser's IWebBrowser2 pointer, which is implemented using the Navigate2 method of the interface, as described in MSDN Navigate2,

HRESULT Navigate2 (
VARIANT *url,
VARIANT *flags,
VARIANT *targetframename,
VARIANT *postdata,
VARIANT *headers
);

Please refer to the MSDN documentation for details of specific parameters. The trouble is the parameter setting of this function, followed by detailed explanation, first look at the code. Here are the following assumptions

Ibrowser is a valid IWebBrowser2 pointer,

The URL is a valid address (for example: L "http://172.24.1.241/profile/"),

PostData for data that needs to be submitted (

such as: L "Userid=kesalin&password=pwd&action=profile")

Floating Clouds (l_zhaohui@163.com) 2007/11/8

Iwebbrowser2* Ibrowser;

LPCTSTR URL;

LPCTSTR PostData

...//other code

Get size of Post Data

int size = WideCharToMultiByte (CP_ACP, 0, PostData,-1, 0, 0, 0, 0);

VARIANT Vurl;

VARIANT Vflags;

VARIANT Vpostdata;

VARIANT vheaders;

VARIANT Vnull;

Init

VariantInit (&vurl);

VariantInit (&vflags);

VariantInit (&vpostdata);

VariantInit (&vheaders);

VariantInit (&vnull);

Set value

VHEADERS.VT = VT_BSTR;

Vheaders.bstrval = SysAllocString (L "content-type:application/x-www-form-urlencoded\r\n");

VFLAGS.VT = VT_I4;

Vflags.lval = Navnoreadfromcache | Navnowritetocache;

VURL.VT = VT_BSTR;

Vurl.bstrval = SysAllocString (URL);

if (Size > 1) {

POST

char* Ppostdata = new Char[size + 1];

WideCharToMultiByte (CP_ACP, 0, PostData,-1, ppostdata, size, 0, 0);

SAFEARRAY far* sfpost = NULL;

Safearraybound bound;

Bound.celements = (ULONG) (strlen (Ppostdata));

Bound.llbound = 0;

Sfpost = SafeArrayCreate (vt_ui1, 1, &bound);

char* PChar = Ppostdata;

for (long lIndex = 0; LIndex < (signed) bound.celements; lindex++) {

SafeArrayPutElement (Sfpost, &lindex, (void*) ((pchar++)));

}

VPOSTDATA.VT = Vt_array | VT_UI1;

Vpostdata.parray = Sfpost;

Delete[] Ppostdata;

Ppostdata = NULL;

PChar = NULL;

Ibrowser->navigate2 (&vurl, &vflags, &vnull, &vpostdata, &vheaders);

}

else {

GET

Ibrowser->navigate2 (&vurl, &vnull, &vnull, &vnull, &vnull);

}

Clear

VariantClear (&vurl);

VariantClear (&vflags);

VariantClear (&vpostdata);

VariantClear (&vheaders);

VariantClear (&vnull);

There are three points worth explaining:

First, the data type VT_I4 is the data that indicates a long type, so set the variant's Lval as the value. As in the code:

VFLAGS.VT = VT_I4;

Vflags.lval = Navnoreadfromcache | Navnowritetocache;

Second, if you want to implement post data submission, you cannot simply set the unnecessary parameters in the Navigate2 method to NULL, you must create a Variant variable vnull, and initialize it. Like what:

VariantInit (&vnull);

VNULL.VT = VT_BSTR;

Vnull.bstrval = NULL;

......

Ibrowser->navigate2 (&vurl, &vflags, &vnull, &vpostdata, &vheaders);

Thirdly, the variable type of Vpostdata is Vt_array | VT_UI1, whose data is a SafeArray character array, refer to the code for specific assignments.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.