青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

huyutian

他強由他強,清風(fēng)拂山崗;他橫由他橫,明月照大江。他自狠來他自惡,我自一口真氣足

  C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
  20 隨筆 :: 47 文章 :: 22 評論 :: 0 Trackbacks

關(guān)于WinInet的異步模式,微軟給過很多例子,但我覺得下面這個是比較好理解的。
原文地址,代碼貼出來,有些細節(jié)部分還要自己慢慢體會。
我在調(diào)試過程中,還參照了以下一些鏈接
關(guān)于GetLastError返回1201(ERROR_INTERNET_INCORRECT_HANDLE_STATE)9錯誤的解決辦法請參見下面兩個鏈接
http://support.genopro.com/Topic14017-59-1.aspx
http://support.microsoft.com/kb/177190
這是微軟wininet的一個bug,僅當(dāng)大 POST請求包要發(fā)送時,使用 HttpSendRequestEx。其他時候使用HttpSendRequest。防止12019錯誤

WinInet 錯誤代碼可參照這里 http://support.microsoft.com/kb/193625

  1#include<windows.h>
  2#include<wininet.h>
  3#include<iostream.h>
  4
  5HANDLE hConnectedEvent, hRequestOpenedEvent, hRequestCompleteEvent;
  6HINTERNET hInstance, hConnect, hRequest;
  7char *lpszUrl, *lpszServer;
  8
  9BOOL bAllDone = FALSE;
 10BOOL bVerbose = FALSE;
 11
 12void __stdcall Callback(HINTERNET hInternet,
 13              DWORD dwContext,
 14              DWORD dwInternetStatus,
 15              LPVOID lpStatusInfo,
 16              DWORD dwStatusInfoLen);
 17
 18void main(int argc, char *argv[])
 19{
 20    if (argc != 3)
 21    {
 22        if ((argc == 4&& (argv[3][0== 'v'))
 23            bVerbose = TRUE;
 24        else
 25        {
 26            cout << "Usage: asynchttp <server> <url> [v]" << endl;
 27            cout << "   <server> is the hostname of the http server" << endl;
 28            cout << "   <url> is the url of the object you are requesting (without the hostname)" << endl;
 29            cout << "   'v' for verbose output" << endl << endl;
 30            cout << "   Example: asynchttp www.domain.com /docs/readme.htm v" << endl;
 31            return;
 32        }

 33    }

 34
 35    lpszServer = argv[1];
 36    lpszUrl = argv[2];
 37
 38    hConnectedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
 39    hRequestOpenedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
 40    hRequestCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
 41
 42    hInstance = InternetOpen("asynchttp"
 43                             INTERNET_OPEN_TYPE_PRECONFIG,
 44                             NULL,
 45                             NULL,
 46                             INTERNET_FLAG_ASYNC); // ASYNC Flag
 47
 48    if (hInstance == NULL)
 49    {
 50        cout << "InternetOpen failed, error " << GetLastError();
 51        return;
 52    }

 53
 54    // Setup callback function
 55    if (InternetSetStatusCallback(hInstance,
 56                                  (INTERNET_STATUS_CALLBACK)&Callback) == INTERNET_INVALID_STATUS_CALLBACK)
 57    {
 58        cout << "InternetSetStatusCallback failed, error " << GetLastError();
 59        return;
 60    }

 61
 62    // First call that will actually complete asynchronously even
 63    // though there is no network traffic
 64    hConnect = InternetConnect(hInstance, 
 65                               lpszServer, 
 66                               INTERNET_DEFAULT_HTTP_PORT,
 67                               NULL,
 68                               NULL,
 69                               INTERNET_SERVICE_HTTP,
 70                               0,
 71                               1); // Connection handle's Context
 72    if (hConnect == NULL)
 73    {
 74        if (GetLastError() != ERROR_IO_PENDING)
 75        {
 76            cout << "InternetConnect failed, error " << GetLastError();
 77            return;
 78        }

 79        // Wait until we get the connection handle
 80        WaitForSingleObject(hConnectedEvent, INFINITE);
 81    }

 82
 83
 84    // Open the request
 85    hRequest = HttpOpenRequest(hConnect, 
 86                               "GET"
 87                               lpszUrl,
 88                               NULL,
 89                               NULL,
 90                               NULL,
 91                               INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE,
 92                               2);  // Request handle's context 
 93    if (hRequest == NULL)
 94    {
 95        if (GetLastError() != ERROR_IO_PENDING)
 96        {
 97            cout << "HttpOpenRequest failed, error " << GetLastError();
 98            return;
 99        }

100        // Wait until we get the request handle
101        WaitForSingleObject(hRequestOpenedEvent, INFINITE);
102    }

103
104    if (!HttpSendRequest(hRequest, 
105                         NULL, 
106                         0
107                         NULL,
108                         0))
109    {
110        if (GetLastError() != ERROR_IO_PENDING)
111        {
112            cout << "HttpSendRequest failed, error " << GetLastError();
113            return;
114        }

115    }

116    
117    if (bVerbose)
118    {
119        cout << "HttpSendRequest called successfully" << endl;
120        cout.flush();
121    }

122
123    WaitForSingleObject(hRequestCompleteEvent, INFINITE);
124
125    cout << "------------------- Read the response -------------------" << endl;
126    char lpReadBuff[256];
127
128    do
129    {
130        INTERNET_BUFFERS InetBuff;
131        FillMemory(&InetBuff, sizeof(InetBuff), 0);
132        InetBuff.dwStructSize = sizeof(InetBuff);
133        InetBuff.lpvBuffer = lpReadBuff;
134        InetBuff.dwBufferLength = sizeof(lpReadBuff) - 1;
135        
136        if (bVerbose)
137        {
138            cout << "Calling InternetReadFileEx" << endl;
139            cout.flush();
140        }

141
142        if (!InternetReadFileEx(hRequest,
143                              &InetBuff,
144                              02))
145        {
146            if (GetLastError() == ERROR_IO_PENDING)
147            {
148                if (bVerbose)
149                {
150                    cout << "Waiting for InternetReadFileEx to complete" << endl;
151                    cout.flush();
152                }

153                WaitForSingleObject(hRequestCompleteEvent, INFINITE);
154            }

155            else
156            {
157                cout << "InternetReadFileEx failed, error " << GetLastError();
158                cout.flush();
159                return;
160            }

161        }

162
163        lpReadBuff[InetBuff.dwBufferLength] = 0;
164        cout << lpReadBuff;
165        cout.flush();
166
167        if (InetBuff.dwBufferLength == 0
168            bAllDone = TRUE;
169
170    }
 while (bAllDone == FALSE);
171
172    cout << endl << endl << "------------------- Request Complete ----------------" << endl;
173
174}

175
176void __stdcall Callback(HINTERNET hInternet,
177              DWORD dwContext,
178              DWORD dwInternetStatus,
179              LPVOID lpStatusInfo,
180              DWORD dwStatusInfoLen)
181{
182    if (bVerbose)
183    {
184        cout << "Callback dwInternetStatus: " << dwInternetStatus << " Context: " << dwContext << endl;
185        cout.flush();
186    }

187
188    switch(dwContext)
189    {
190    case 1// Connection handle
191        if (dwInternetStatus == INTERNET_STATUS_HANDLE_CREATED)
192        {
193            INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
194            hConnect = (HINTERNET)pRes->dwResult;
195            if (bVerbose)
196            {
197                cout << "Connect handle created" << endl;
198                cout.flush();
199            }

200            SetEvent(hConnectedEvent);
201        }

202        break;
203    case 2// Request handle
204        switch(dwInternetStatus)
205        {
206        case INTERNET_STATUS_HANDLE_CREATED:
207            {
208                INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
209                hRequest = (HINTERNET)pRes->dwResult;
210                if (bVerbose)
211                {
212                    cout << "Request handle created" << endl;
213                    cout.flush();
214                }

215                SetEvent(hRequestOpenedEvent);
216            }

217            break;
218        case INTERNET_STATUS_REQUEST_SENT:
219            {
220                DWORD *lpBytesSent = (DWORD*)lpStatusInfo;
221                if (bVerbose)
222                {
223                    cout << "Bytes Sent: " << *lpBytesSent << endl;
224                    cout.flush();
225                }

226            }

227            break;
228        case INTERNET_STATUS_REQUEST_COMPLETE:
229            {
230                INTERNET_ASYNC_RESULT *pAsyncRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
231                if (bVerbose)
232                {
233                    cout << "Function call finished" << endl;
234                    cout << "dwResult: " << pAsyncRes->dwResult << endl;
235                    cout << "dwError:  " << pAsyncRes->dwError << endl;
236                    cout.flush();
237                }

238                SetEvent(hRequestCompleteEvent);
239            }

240            break;
241        case INTERNET_STATUS_RECEIVING_RESPONSE:
242            if (bVerbose)
243            {
244                cout << "Receiving Response" << endl;
245                cout.flush();
246            }

247            break;
248        case INTERNET_STATUS_RESPONSE_RECEIVED:
249            {
250                DWORD *dwBytesReceived = (DWORD*)lpStatusInfo;
251                if (*dwBytesReceived == 0)
252                    bAllDone = TRUE;
253                if (bVerbose)
254                {
255                    cout << "Received " << *dwBytesReceived << endl;
256                    cout.flush();
257                }

258            }

259
260        }

261
262    }

263
264}

265
posted on 2010-08-10 22:52 胡雨田 閱讀(5454) 評論(0)  編輯 收藏 引用 所屬分類: 網(wǎng)絡(luò)編程
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美一区二区三区四区在线| 亚洲直播在线一区| 亚洲人成啪啪网站| 亚洲综合国产| 亚洲一区二区三区在线看| 久久婷婷av| 国产精品久久久久永久免费观看 | 中文在线一区| 免费久久久一本精品久久区| 国产精品日韩久久久| 亚洲级视频在线观看免费1级| 亚洲一二三区在线| 亚洲国产精品久久久久秋霞影院| 亚洲欧美中文日韩v在线观看| 久久只精品国产| 午夜精品视频网站| 国产精品久久77777| 亚洲剧情一区二区| 欧美jjzz| 久久久久高清| 国内一区二区在线视频观看| 午夜在线一区二区| 中文一区二区| 韩国欧美一区| 亚洲免费观看高清完整版在线观看熊 | 国产精品不卡在线| 夜夜狂射影院欧美极品| 欧美成人精品影院| 欧美精品aa| 99亚洲视频| 欧美亚洲日本一区| 99国产精品久久久久老师| 亚洲国产福利在线| 欧美成人黑人xx视频免费观看| 欧美黄色精品| 亚洲午夜免费视频| 久久久久久穴| 亚洲精品九九| 最新日韩精品| 亚洲欧美日韩一区二区三区在线观看 | 日韩亚洲欧美在线观看| 国产精品99久久久久久久久| 永久91嫩草亚洲精品人人| 欧美在线免费| 欧美一区网站| 欧美日韩在线播放| 一区二区三区四区精品| 亚洲精选在线观看| 国产精品久久99| 欧美国产精品久久| 欧美精品尤物在线| 亚洲欧美另类在线| 久久精品一区蜜桃臀影院| 在线欧美日韩| 日韩午夜激情| 亚洲巨乳在线| 欧美11—12娇小xxxx| 久久色中文字幕| 欧美成年网站| 欧美承认网站| 亚洲福利专区| 99精品欧美一区二区蜜桃免费| 国产伦精品一区二区三区照片91 | 亚洲午夜av在线| 国内精品久久久久久久影视麻豆 | 亚洲毛片在线观看.| 亚洲精品久久久久中文字幕欢迎你| 久久精品国内一区二区三区| 久久精品夜色噜噜亚洲aⅴ| 久久亚洲色图| 欧美jizz19性欧美| 亚洲精品美女| 欧美日本亚洲视频| 亚洲私人影院| 久久超碰97中文字幕| 国产视频一区在线| 亚洲毛片av在线| 亚洲调教视频在线观看| 久久精品盗摄| 亚洲综合色网站| 国产精品美腿一区在线看| 亚洲图片在线| 久久精品视频播放| 久久久久国产精品人| 国产精品你懂得| 欧美呦呦网站| 亚洲二区在线观看| 亚洲在线视频免费观看| 国产欧美一区二区精品忘忧草| 亚洲国产成人91精品| 在线视频你懂得一区二区三区| 欧美视频在线观看 亚洲欧| 欧美成人精品1314www| 日韩午夜激情| 国产色视频一区| 乱人伦精品视频在线观看| 久久久之久亚州精品露出| 亚洲电影免费在线观看| 欧美日韩成人激情| 亚洲精品美女久久久久| 亚洲精品日日夜夜| 欧美性感一类影片在线播放| 香蕉久久夜色精品| 亚洲国产高清aⅴ视频| 欧美一级黄色网| 最新国产の精品合集bt伙计| 国产精品久久久久一区| 麻豆九一精品爱看视频在线观看免费| 亚洲精品一品区二品区三品区| 欧美一区高清| 国产手机视频精品| 欧美激情视频在线播放| 性欧美暴力猛交69hd| 91久久精品国产91性色| 久久久亚洲国产美女国产盗摄| 亚洲激情另类| 国内免费精品永久在线视频| 欧美色图麻豆| 欧美成人午夜77777| 久久精品三级| 亚洲欧美久久久久一区二区三区| 久久精品欧洲| 亚洲一区二区免费视频| 亚洲精品系列| 在线观看亚洲精品视频| 国产精品热久久久久夜色精品三区| 老牛嫩草一区二区三区日本| 亚洲欧洲日产国产网站| 麻豆精品精华液| 亚洲免费观看高清完整版在线观看熊 | 亚洲精品一二区| 欧美成年人网| 久久亚洲春色中文字幕| 欧美一级久久| 欧美一区二区三区免费观看| 中文日韩在线视频| 在线视频欧美日韩精品| 日韩视频免费| 亚洲精品资源美女情侣酒店| 136国产福利精品导航| 黄色一区二区在线| 欧美久色视频| 欧美精品在线观看91| 欧美电影电视剧在线观看| 美女网站在线免费欧美精品| 久久久蜜桃一区二区人| 久久久99精品免费观看不卡| 久久国产精品黑丝| 久久久精品一区| 久久夜色撩人精品| 欧美 亚欧 日韩视频在线| 免费观看亚洲视频大全| 欧美成人中文字幕| 欧美精品久久一区| 欧美天天影院| 国产精品视频免费| 国产亚洲精品久久久久婷婷瑜伽| 欧美激情1区2区3区| 欧美精品久久久久久久| 欧美日韩国产高清| 欧美午夜大胆人体| 91久久黄色| aa成人免费视频| 亚洲综合国产| 久久久av水蜜桃| 欧美国产高清| 国产精品一卡| 亚洲高清资源| 亚洲一区视频在线| 欧美一区二区三区免费视频| 久久夜色精品国产欧美乱| 欧美激情视频给我| 老司机精品视频一区二区三区| 欧美成人tv| 在线视频亚洲| 久久久久久久久久久一区| 欧美寡妇偷汉性猛交| 国产精品久久| 在线欧美亚洲| 亚洲在线电影| 欧美成人精精品一区二区频| 99视频一区| 久久久另类综合| 国产精品激情偷乱一区二区∴| 国产在线播精品第三| 亚洲精品一二三| 久久久亚洲高清| 一区二区激情| 一本色道久久88精品综合| 午夜精品偷拍| 欧美日韩国产精品一区二区亚洲| 国产日韩欧美电影在线观看| 亚洲日本aⅴ片在线观看香蕉| 亚洲欧美日韩视频一区| 欧美成人69av| 欧美在线观看视频在线| 欧美小视频在线| 亚洲肉体裸体xxxx137| 久久九九精品99国产精品| 99视频一区二区|