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的简单使用 有 38 条回应

  1. Asking questions are in fact pleasant thing if you are not understanding something fully, except this
    post gives nice understanding even.

    回复
  2. Excellent way of explaining, and pleasant post to take
    data about my presentation subject, which i am going to
    present in school.

    回复
  3. Hi there Dear, are you really visiting this site regularly, if so then you will without doubt take pleasant knowledge.

    回复
  4. What’s up, just wanted to say, I loved this blog post.
    It was funny. Keep on posting!

    回复
  5. When someone writes an post he/she keeps the thought
    of a user in his/her mind that how a user can be aware of it.
    Thus that’s why this paragraph is perfect. Thanks!

    回复
  6. My relatives always say that I am wasting my time here at web, but I know I am getting knowledge daily by reading such good articles or
    reviews.

    回复
  7. My developer is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the expenses.

    But he’s tryiong none the less. I’ve been using WordPress on a number of
    websites for about a year and am worried about switching to another platform.
    I have heard great things about blogengine.net. Is there
    a way I can transfer all my wordpress posts into it? Any kind of help would be greatly appreciated!

    回复
  8. This is very interesting, You’re a very skilled blogger. I have joined your feed and look forward to seeking more of your magnificent post.
    Also, I have shared your web site in my social networks!

    回复
  9. obviously like your web site however you have to test the spelling on several of your posts.
    Several of them are rife with spelling problems and I to find it very bothersome to inform the truth on the other
    hand I will certainly come back again.

    回复
  10. viagra plus for discount

    回复
  11. good choice is headaches

    af29 muscle pain caused by is

    回复
  12. achter generique du is

    4a57 what is shelf life of is

    回复
  13. viagra en los jovene

    回复
  14. how to be is in dublin

    70e5 price is vs is

    回复
  15. at is with mastercard

    82f8 at is silagra

    回复
  16. levitra 20 mg o is

    回复
  17. buy viagra 25mg tablets

    回复
  18. viagra in toronto office

    回复
  19. viagra discounts

    回复
  20. viagra online rosario

    回复
  21. memenucom buy viagra mexico

    回复
  22. viagra 100 mg 4 cpr

    回复
  23. click here viagra soft

    回复
  24. levitra 20 mg o viagra

    回复
  25. is wirkung bei frau

    fe25 is a 50 mg

    回复
  26. is dosaggio consigliato

    05d6 is generique uk suisse

    回复
  27. is discount price us

    05d6 is comprimidos

    回复
  28. best ib kube tabs vis

    9721 be is in perth australia

    回复
  29. is hta

    3570 be is in florida on line

    回复
  30. is dosage size

    10c8 can you order is

    回复
  31. is bruising

    9509 compare is price

    回复
  32. viagra pricelist

    回复
  33. 069c comprar viagra en argentina

    does viagra do men women

    回复
  34. buy viagra online free

    回复
  35. online generic viagra sales

    回复
  36. 069c viagra sales pitch

    try it viagra uk buy online

    回复
  37. viagra online kaufende

    回复

发表评论

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