LSX-blog

Feed Rss

【收录】CInternetSession的简单使用

07.01.2011, Win32, by .

1. CInternetSession的简单使用
CInternetSession session;
CHttpFile *file = NULL;
CString strURL = " http://www.lishixin.net";
CString strHtml = "”; //存放网页数据
try{
file = (CHttpFile*)session.OpenURL(strURL);
}catch(CInternetException * m_pException){
file = NULL;
m_pException->m_dwError;
m_pException->Delete();
session.Close();
MessageBox("CInternetException");
}
CString strLine;
if(file != NULL){
while(file->ReadString(strLine) != NULL){
strHtml += strLine;
}
}else{
MessageBox("fail");
}
session.Close();
file->Close();
delete file;
file = NULL;


2. CInternetSession的代理与超时使用
CInternetSession session;
CHttpFile *file = NULL;
INTERNET_PROXY_INFO proxyinfo;
proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxyinfo.lpszProxy ="211.104.243.73:8080";
proxyinfo.lpszProxyBypass = NULL;
session.SetOption(INTERNET_OPTION_PROXY,(LPVOID)&proxyinfo,
sizeof(INTERNET_PROXY_INFO));
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 5000); // 5秒的连接超时
session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000); // 1秒的发送超时
session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 7000); // 7秒的接收超时
session.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000); // 1秒的发送超时
session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 7000); // 7秒的接收超时
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); // 1次重试
try{
file = (CHttpFile*)session.OpenURL("http://www.163.com",1,
INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE);
}catch(CInternetException * m_pException){
file = NULL;
m_pException->m_dwError;
m_pException->Delete();
session.Close();
MessageBox("CInternetException");
return;
}
CString strLine;
if(file != NULL){
while(file->ReadString(strLine) != NULL){
MessageBox(strLine);
}
}else{
MessageBox("fail");
}
file->Close();
session.Close();

3. CInternetSession 的POST使用

CInternetSession m_InetSession(_T("session"),
0,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_DONT_CACHE); //设置不缓冲
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString strHtml = "";
CString strRequest = "name=123&pwd=321\r\n"; //POST过去的数据
CString strHeaders = "Accept: */*\r\nReferer: http://www.goodwaiter.com/\r\nAccept-Language: zh-cn\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
try{
INTERNET_PORT nPort; //端口
nPort=80;
pServer = m_InetSession.GetHttpConnection("www.goodwaiter.com", nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/");
pFile->AddRequestHeaders(strHeaders);
pFile->SendRequestEx(strRequest.GetLength());
pFile->WriteString(strRequest); //重要-->m_Request 中有"name=aaa&name2=BBB&..."
pFile->EndRequest();
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
if (dwRet == HTTP_STATUS_OK){
CString strLine;
while ((nRead = pFile->ReadString(strLine))>0)
{
strHtml += strLine;
}
}
delete pFile;
delete pServer;
}
catch (CInternetException* e){
e->m_dwContext;
}

【收录】CInternetSession的简单使用 有 70 条回应

  1. What i don’t realize is in fact how you’re no longer really a lot more smartly-preferred than you may be right now. You are so intelligent. You already know therefore significantly in the case of this matter, made me for my part believe it from a lot of numerous angles. Its like men and women aren’t fascinated except it’s something to accomplish with Girl gaga! Your personal stuffs great. Always take care of it up!

    回复
  2. I am sure this paragraph has touched all the internet visitors,
    its really really pleasant article on building up new webpage.

    回复
  3. May I just say what a comfort to discover somebody who truly understands what they’re discussing on the internet.
    You actually realize how to bring a problem to light and make it
    important. More people need to check this out and understand this side of the story.
    I was surprised that you’re not more popular since you most certainly have the
    gift.

    回复
  4. [url=http://levitrav.com/]levitra online canada[/url] [url=http://iventolin.com/]buy ventolin[/url] [url=http://diclofenacg.com/]diclofenac topical gel[/url] [url=http://robaxinmed.com/]robaxin 750 mg tabs[/url] [url=http://sildenafilv.com/]sildenafil 100 mg tablet[/url] [url=http://baclofenonline.com/]baclofen 10[/url] [url=http://indocinrx.com/]indocin 50 mg cap[/url] [url=http://advairdisk.com/]order advair online cheap[/url]

    回复
  5. Hurrah! After all I got a webpage from where I know how to genuinely obtain valuable facts concerning my study
    and knowledge.

    回复
  6. “This site was… how do you say it? Relevant!! Finally I’ve found something that helped me. Cheers!”

    回复
  7. [url=http://zovirax24.com/]zovirax over the counter cream[/url] [url=http://silagratab.com/]silagra online[/url] [url=http://cephalexinkeflex.com/]cephalexin 250 mg capsule[/url] [url=http://allopurinolmed.com/]allopurinol 100mg tablets[/url]

    回复
  8. [url=https://hydroxychloroquine911.com/]hydroxychloroquine sulfate[/url]

    回复
  9. Hey there would you mind letting me know which web host you’re working with? I’ve loaded your blog in 3 completely different browsers and I must say this blog loads a lot quicker then most. Can you recommend a good web hosting provider at a reasonable price? Thanks a lot, I appreciate it!|

    回复
  10. [url=http://ampicillin.us.org/]ampicillin price[/url] [url=http://buycelexa.us.com/]celexa buy online[/url] [url=http://baclofen.us.com/]purchase baclofen[/url] [url=http://tadalafil247.us.org/]tadalafil 6mg[/url] [url=http://viagrabest.us.com/]buy viagra[/url] [url=http://anafranil.us.com/]buy anafranil[/url]

    回复
  11. Hmm is anyone else encountering problems with the pictures on this blog loading?
    I’m trying to determine if its a problem on my end or if
    it’s the blog. Any responses would be greatly appreciated.

    回复
  12. Heya i’m for the first time here. I came across this board and I find It truly
    useful & it helped me out much. I hope to give something back and help others like you aided me.

    回复
  13. I like it when folks get together and share opinions.
    Great website, stick with it!

    回复
  14. I am actually thankful to the owner of this web site who has shared this impressive post at at this
    place.

    回复
  15. I blog frequently and I really thank you for your content.
    This article has really peaked my interest. I will bookmark your site and keep checking for new details about once per week.

    I opted in for your Feed too.

    回复
  16. Ahaa, its good discussion on the topic of this post
    at this place at this weblog, I have read all that,
    so at this time me also commenting at this place.

    回复
  17. Everything is very open with a precise clarification of the issues.
    It was really informative. Your website is useful.
    Thank you for sharing!

    回复
  18. Hi i am kavin, its my first occasion to commenting
    anyplace, when i read this post i thought i could also make comment due to this good post.

    回复
  19. Generally I don’t learn article on blogs, but I would like to say that this write-up very pressured me to check out and do so!
    Your writing style has been amazed me. Thanks, quite nice post.

    回复
  20. Hi, i think that i saw you visited my website thus i came to “return the favor”.I am trying to find things to improve my site!I suppose its ok to
    use some of your ideas!!

    回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注