2009年4月24日星期五

在VC中WININET如何使用HTTP的POSTA和GET方法

在VC中WININET如何使用HTTP的POSTA和GET方法 2009-02-13 16:35
分类:默认分类
字号: 大大 中中 小小

Post资料:

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 '!'.

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&Q");

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&Q");

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 request的原始信息,不过使用Inet API做的,希望能有帮助。

void CHTTPRequestDlg::OnButtonRequest()

{

UpdateData(TRUE);

HINTERNET hInternet = InternetOpen("Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0)",

INTERNET_OPEN_TYPE_DIRECT,

NULL,

NULL,

NULL);

HINTERNET hSession = InternetConnect(hInternet,

m_strHost,

m_nPort,

"username",

"password",

INTERNET_SERVICE_HTTP,

0,

0);

char* szAccept[] = {"*/*",NULL};

CString strVerb;

m_comboVerb.GetWindowText(strVerb);

HINTERNET hRequest = HttpOpenRequest(hSession,

strVerb,

m_strObject,

NULL,

NULL,

(LPCSTR*)szAccept,

0,

0);

struct

{

char* Language;

char* Encoding;

char* ContentType;

}Headers = {"Accept-Language:zh-cn\r\n",

"Accept-Encoding:gzip,deflate\r\n",

"Content-Type:application/x-www-form-urlencoded\r\n"};



if(m_bLanguage)

{

HttpAddRequestHeaders(hRequest,

Headers.Language,

-1,

HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);

}

if(m_bEncoding)

{

HttpAddRequestHeaders(hRequest,

Headers.Encoding,

-1,

HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);

}

if(m_bContentType)

{

HttpAddRequestHeaders(hRequest,

Headers.ContentType,

-1,

HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE);

}

LPCSTR lpAddHeader = NULL;

LPCSTR lpContent = NULL;

if(m_strHeaders.GetLength())

{

if(m_strHeaders.Right(2) != "\r\n")

{

m_strHeaders+="\r\n";

}

lpAddHeader = (LPCSTR)m_strHeaders;

}

if(m_strContent.GetLength() && (strVerb == "POST" || strVerb == "PUT"))

{

lpContent = (LPCSTR)m_strContent;

}

HttpSendRequest(hRequest,

lpAddHeader,

-1,

(LPVOID)lpContent,

m_strContent.GetLength());



m_editContentGot.SetSel(0,-1);

m_editContentGot.ReplaceSel("");



LPSTR lpszData; //buffer for the data

DWORD dwSize; //size of the data available

DWORD dwDownloaded; //size of the downloaded data



//Set the cursor to an hourglass.

SetCursor(LoadCursor(NULL,IDC_WAIT));



// This loop handles reading the data.

while(1)

{

// The call to InternetQueryDataAvailable determines the amount of

// data available to download.

if (!InternetQueryDataAvailable(hRequest,&dwSize,0,0))

{

SetCursor(LoadCursor(NULL,IDC_ARROW));

break;

}

else

{

// Allocates a buffer of the size returned by InternetQueryDataAvailable

lpszData = new char[dwSize+1];



// Reads the data from the HINTERNET handle.

if(!InternetReadFile(hRequest,(LPVOID)lpszData,dwSize,&dwDownloaded))

{

delete[] lpszData;

break;

}

else

{

// Adds a null terminator to the end of the data buffer

lpszData[dwDownloaded]='\0';



int nLen = m_editContentGot.GetWindowTextLength();

m_editContentGot.SetSel(nLen-1, nLen-1);

m_editContentGot.ReplaceSel(lpszData);



// Delete the two buffers

delete[] lpszData;



// Check the size of the remaining data. If it is zero, break.

if (dwDownloaded == 0)

{

break;

}

}

}

}



// Close the HINTERNET handle

InternetCloseHandle(hRequest);

InternetCloseHandle(hSession);

InternetCloseHandle(hInternet);



// Set the cursor back to an arrow

SetCursor(LoadCursor(NULL,IDC_ARROW));

}

==========================================

关文件:#include

使用MFC示例如下:

首先设置m_strRequest请求字符串 eg."name=aaa&pass=bbb";

m_strServerName 服务器名称或者IP eg."www.yahoo.com"

m_strObjectName 请求文件位置 eg. "pub/aaa.asp"

请求的结果存放在m_strHtml中

void func()

{

CInternetSession m_InetSession("session");

CHttpConnection *pServer = NULL;

CHttpFile* pFile = NULL;

try

{

INTERNET_PORT nPort;

nPort=80;

pServer = m_InetSession.GetHttpConnection(m_strServerName, nPort);

pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,m_strObjectName);

char szHeaders[100];

strcpy(szHeaders,"Accept: text*/*\r\nContent-Type: application/x-www-form-urlencoded");

pFile->AddRequestHeaders(szHeaders);



pFile->SendRequestEx(m_strRequest.GetLength());

pFile->WriteString(m_strRequest);//重要-->m_Request 中有"name=aaa&name2=BBB&"

pFile->EndRequest();

DWORD dwRet;

pFile->QueryInfoStatusCode(dwRet);

CString str;



//m_Mutex.Lock();

m_strHtml="";

char szBuff[1024];

if (dwRet == HTTP_STATUS_OK)

{

UINT nRead;

while ((nRead = pFile->Read(szBuff,1023))>0)

{

m_strHtml += CString(szBuff,nRead);

}

}

//m_Mutex.Unlock();



delete pFile;

delete pServer;

}

catch (CInternetException* e)

{

CString s;

s.Format("Internet Exception\r\nm_dwError%u,m_dwContextError%u",e->m_dwError,e->m_dwContext);

AfxMessageBox(s);

//catch errors from WinInet

}

}

============================

1、获得WebBrowser Control的DWebBrowserEvents2::DocumentComplete事件

2、在DWebBrowserEvents2::DocumentComplete事件中根据IWebBrowser2::Document获得IHTMLDocument2

3、IHTMLDocument2::forms得到IHTMLElementCollection

4、在IHTMLElementCollection中根据name、tagName、ID得到指定的IHTMLElement

5、从IHTMLElement得到IHTMLFormElement

6、执行IHTMLFormElement::submit

==================================================

要正确模拟表单提交使用 WinInet, 您需要发送一个指示正确 Content-Type 标头。 对于窗体, 正确 Content-Type 标题是:

Content-Type: application/x-www-form-urlencoded

如果没有指定 Content-Type 在许多情况下, 服务器不响应正确。 在之前将表单变量添加到 " Request.Form "

对象例如, IIS 3.0 的 ActiveServerPages 组件实际上检查专门用于应用表单 ' www 程序 / x -

urlencoded ' 此标题。 此 MIME / 内容类型指示请求数据是 - URL 编码窗体变量的列表。 URL 编码意味着空格字符

(ASCII 32) 被编码为 + ' ', 特殊字符如 ' ! ' 以十六进制形式编码为 21 % '。

下面是使用 MFCWinInet 类来模拟 FormPOST 请求的代码段:

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



没有 MFC, 相同的代码将转换为直 SDK 调用如下:

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

===================================================



Get资料:

#include "winsock2.h"

#include "stdio.h"

#include "Wininet.h"

#pragma comment (lib,"Wininet.lib")

void main()

{

HINTERNET hNet = ::InternetOpen("Google page", //当HTTP协议使用时,这个参数随意赋值

PRE_CONFIG_INTERNET_ACCESS, //访问类型指示Win32网络函数使用登记信息去发现一个服务器。

NULL,

INTERNET_INVALID_PORT_NUMBER, //使用INTERNET_INVALID_PORT_NUMBER相当于提供却省的端口数。

0 //标志去指示使用返回句句柄的将来的Internet函数将"不"为回调函数发送状态信息

) ;

HINTERNET hUrlFile = ::InternetOpenUrl(hNet, //从InternetOpen返回的句柄

"http://www.google.com", //需要打开的URL

NULL, //用来向服务器传送额外的信息,一般为NULL

0, //用来向服务器传送额外的信息,一般为 0

INTERNET_FLAG_RELOAD, //InternetOpenUrl行为的标志

0) ; //信息将不会被送到状态回调函数

char buffer[1024] ;

DWORD dwBytesRead = 0;

BOOL bRead = ::InternetReadFile(hUrlFile, //InternetOpenUrl返回的句柄

buffer, //保留数据的缓冲区

sizeof(buffer),

&dwBytesRead); //指向包含读入缓冲区字节数的变量的指针;

//如果返回值是TRUE,而且这里指向0,则文件已经读到了文件的末尾。

::InternetCloseHandle(hUrlFile) ;

::InternetCloseHandle(hNet) ;

printf("%s", buffer);

}

8 条评论:

匿名 说...

[u][b]Xrumer[/b][/u]

[b]Xrumer SEO Professionals

As Xrumer experts, we secure been using [url=http://www.xrumer-seo.com]Xrumer[/url] quest of a sustained immediately conditions and know how to harness the enormous power of Xrumer and turn it into a Cash machine.

We also purvey the cheapest prices on the market. Many competitors will charge 2x or even 3x and a end of the term 5x what we debt you. But we feel in providing prominent help at a debilitated affordable rate. The unbroken incidental of purchasing Xrumer blasts is because it is a cheaper variant to buying Xrumer. So we focusing to abide by that mental activity in rebuke and provide you with the cheapest rate possible.

Not just do we have the unexcelled prices but our turnaround in the good old days b simultaneously for the treatment of your Xrumer posting is wonderful fast. We will secure your posting done to come you know it.

We also produce you with a full log of well-heeled posts on manifold forums. So that you can see seeking yourself the power of Xrumer and how we get harnessed it to gain your site.[/b]


[b]Search Engine Optimization

Using Xrumer you can wish to see thousands upon thousands of backlinks for your site. Myriad of the forums that your Place you intent be posted on oblige high PageRank. Having your link on these sites can deep down help establish up some cover grade help links and as a matter of fact as well your Alexa Rating and Google PageRank rating utterly the roof.

This is making your site more and more popular. And with this increase in reputation as superbly as PageRank you can think to witness your site definitely superiority gamy in those Search Motor Results.
Traffic

The amount of transportation that can be obtained by harnessing the power of Xrumer is enormous. You are publishing your site to tens of thousands of forums. With our higher packages you may even be publishing your locality to HUNDREDS of THOUSANDS of forums. Ponder 1 mail on a all the rage forum drive by get 1000 or so views, with signify 100 of those people visiting your site. These days create tens of thousands of posts on celebrated forums all getting 1000 views each. Your freight liking go because of the roof.

These are all targeted visitors that are interested or exotic nearly your site. Imagine how assorted sales or leads you can achieve with this titanic gang of targeted visitors. You are line for line stumbling upon a goldmine primed to be picked and profited from.

Keep in mind, Shipping is Money.
[/b]

GET YOUR INFERIOR ERUPTION TODAY:


http://www.xrumer-seo.com

匿名 说...

[B]NZBsRus.com[/B]
Lose Idle Downloads With NZB Files You Can Rapidly Find HD Movies, Console Games, Music, Applications & Download Them @ Rapid Speeds

[URL=http://www.nzbsrus.com][B]Newsgroup[/B][/URL]

匿名 说...

Infatuation casinos? weed excuse this culmination [url=http://www.realcazinoz.com]casino[/url] advisor and include the r“le for the duration of a tack up online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also go over our up to the newborn [url=http://freecasinogames2010.webs.com]casino[/url] command at http://freecasinogames2010.webs.com and assembly existent spondulix !
another luxury [url=http://www.ttittancasino.com]casino spiele[/url] link is www.ttittancasino.com , preferably than of german gamblers, incline via hire into the open abolished online casino bonus.

匿名 说...

prefect as a substitute inasmuch as of all to dissect this unfettered [url=http://www.casinoapart.com]casino[/url] surprisingly at the interdict [url=http://www.casinoapart.com]online casino[/url] criterion with 10's of in style [url=http://www.casinoapart.com]online casinos[/url]. upon case in [url=http://www.casinoapart.com/articles/play-roulette.html]roulette[/url], [url=http://www.casinoapart.com/articles/play-slots.html]slots[/url] and [url=http://www.casinoapart.com/articles/play-baccarat.html]baccarat[/url] at this [url=http://www.casinoapart.com/articles/no-deposit-casinos.html]no pike away casino[/url] , www.casinoapart.com
the finest [url=http://de.casinoapart.com]casino[/url] with a lug UK, german and all wonderful the world. so in stardom of the choicest [url=http://es.casinoapart.com]casino en linea[/url] basket us now.

匿名 说...

It isn't hard at all to start making money online in the hush-hush world of [URL=http://www.www.blackhatmoneymaker.com]blackhat community[/URL], Don’t feel silly if you haven’t heard of it before. Blackhat marketing uses alternative or misunderstood avenues to build an income online.

匿名 说...

[url=http://sverigeonlinecasino.net/ ]online casino [/url]The world has long known his character. History will always retain him as http://onlinecasinose25.com online casino zorro internet casino --Oh! oui, très bien.

匿名 说...

Even when you are sanctioned, you ought to accept to will soon be transferred in your account current. [url=http://paydayloanstrue.co.uk]payday loans[/url] All you need to get started are a few paystubs, two or a Establishment aberrance between loans to acknowledge archetypal. For adjacent account of this aid, you also can apply all through the newfangled online air brush busy world, you'll want accouterments done fast.

匿名 说...

top [url=http://www.c-online-casino.co.uk/]uk casino[/url] brake the latest [url=http://www.casinolasvegass.com/]free casino games[/url] autonomous no store hand-out at the leading [url=http://www.baywatchcasino.com/]casino
[/url].