ふと為替の自動取引をやりたいなーて思って手始めにhttpからデータの読み込みっと
cppでhtmlをテキストデータで一定時間おきに下記出したいなーって考えて調べに調べて
8時間経過.......
c++はネット系に強いって噂をどっかで聞いた覚えがあって調べたところ
標準の"wininet.h"をインクルードしすると
InternetOpen()とInternetOpenUrl()というのが使えるみたいです.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HINTERNET InternetOpen( | |
IN LPCTSTR Agent, | |
IN DWORD AccessType, | |
IN LPCTSTR ProxyName, | |
IN LPCSTR ProxyBypass, | |
IN DWORD Flags); | |
HINTERNET InternetOpenUrl( | |
IN HINTERNET hInternet, | |
IN LPCTSTR Url, | |
IN LPCTSTR Headers OPTIONAL, | |
IN DWORD HeadersSize, | |
IN DWORD Flags, | |
IN DWORD Context); |
使い方としてはこんな感じ↓
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* URLのオープン */ | |
hFile = InternetOpenUrl( | |
hInternet, | |
TEXT("http://www.geocities.co.jp/SiliconValley-PaloAlto/5920/other.html"), | |
NULL, | |
0, | |
INTERNET_FLAG_RELOAD, | |
0); |
でもこれはURLがLPTSTR型だからよくわからない.
僕の場合URLを可変にしたいので
できればchar型がありがたい.
そこでこんな感じにしてみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char* URL = "http://www.google.com"; | |
LPTSTR URLName= (LPTSTR)URL; | |
/* URLのオープン */ | |
hFile = InternetOpenUrl( | |
hInternet, | |
URLName, | |
NULL, | |
0, | |
INTERNET_FLAG_RELOAD, | |
0); |
char型とLPTSTR型は文字コードが違うらしく文字化けてURLがうまく渡されてないみたい.
そこでプロジェぅとのプロパティから
構成プロパティ>全般>文字セット
を「Unicode文字セット」から「マルチバイト文字」に変更するとうまくいくらしい.
うまく行った
これで後はhtmlの内容を読み込んで煮るなり焼くなりご自由にと.
参考までにつかったコード載せときます.
コピペすれば使えると思います.
Source.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <wininet.h> | |
#pragma comment( lib, "Wininet.lib" ) | |
int main(int argc, char** argv){ | |
HINTERNET hInternet; | |
HINTERNET hFile; | |
char Buf[1024]; | |
DWORD ReadSize; | |
BOOL bResult; | |
char* URL = "http://www.google.com"; | |
LPTSTR URLName= (LPTSTR)URL; | |
/* WININET初期化 */ | |
hInternet = InternetOpen( | |
TEXT("WININET Sample Program"), | |
INTERNET_OPEN_TYPE_PRECONFIG, | |
NULL, | |
NULL, | |
0); | |
/* URLのオープン */ | |
hFile = InternetOpenUrl( | |
hInternet, | |
URLName, | |
NULL, | |
0, | |
INTERNET_FLAG_RELOAD, | |
0); | |
/* オープンしたURLからデータを(1000バイトずつ)読み込む */ | |
while(true) | |
{ | |
ReadSize = 1000; | |
bResult = InternetReadFile( | |
hFile, | |
Buf, | |
ReadSize, | |
&ReadSize); | |
/* 全て読み込んだらループを抜ける */ | |
if (bResult && (ReadSize == 0)) | |
{ | |
break; | |
} | |
Buf[ReadSize] = '\0'; | |
printf("%s", Buf); | |
} | |
/* 後処理 */ | |
InternetCloseHandle(hFile); | |
InternetCloseHandle(hInternet); | |
return 0; | |
} |
コメント
コメントを投稿