2010-05-20 2 views
0

검색 문자열 (예 : http://sw.mycompany.com/~tools/cgi-bin/script.cgi?param1=value1&param2=value2&param3=value3 형태로)과 함께 CGI 스크립트가 실행되는 웹 페이지를 쿼리하고 내 애플리케이션에 결과를 표시하는 옵션을 탐색 중입니다.). 내 응용 프로그램은 MFC C++로 작성되었으며 이전에 네트워크 프로그래밍과 관련된 어떠한 시도도 해본 적이 없다고 고백해야합니다.MFC (MSVC 2008)로 작성된 앱에서 CGI 기반 웹 서버를 쿼리하고 결과를 처리하는 방법은 무엇입니까?

내가 할 수없는 일은 실행 불가능한 것입니까? 그렇지 않다면,이 문제를 해결하기 위해 내가 볼 필요가있는 자원을 누군가가 가리킬 수 있습니까?

감사합니다.

답변

1
CInternetSession internet_session; 

CHttpConnection* connection = NULL; 
CHttpFile* file = NULL; 
TRY { 
    connection = internet_session.GetHttpConnection("www.somehost.com", (INTERNET_PORT)80); 
    // URI needs to be a relative path here 
    file = connection->OpenRequest(CHttpConnection::HTTP_VERB_GET, "/some/file/on/server", NULL, 1, NULL, "HTTP/1.1"); 
    BOOL send_result = file->SendRequest(); 
    if (send_result == FALSE) { 
     // When can this happen? Only when connection fails between this and the previous call. 
     // Need to use ugly MFC exception stuff because OpenRequest et al. use it. 
     ::AfxThrowInternetException(internet_session.GetContext()); 
    } 
} 
CATCH (CInternetException, e) { 
    if (file) { 
     file->Close(); 
    } 
    delete connection; 
    file = NULL; 
    connection = NULL; 
} 
END_CATCH 

if (!file) { 
    delete file; 
    delete connection; 
    return false; 
} 

DWORD status_code; 
file->QueryInfoStatusCode(status_code); 
if (status_code != 200) { 
    CString result; 
    if (status_code == 403) { 
     result.Format("Authentication error (HTTP 403)"); 
    } else if (status_code == 404) { 
     result.Format("Object not found (HTTP 404)"); 
    } else if (status_code == 500) { 
     result.Format("Application error: malformed request (HTTP 500)"); 
    } else { 
     result.Format("Got unsupported HTTP status code %d", status_code); 
    } 
    file->Close(); 
    delete file; 
    delete connection; 
    return false; 
} 
1

MFC는 Http 클라이언트를 빌드 할 수 있습니다. 자세한 내용은 MSDN의 article을 참조하십시오.

question도 유용 할 수 있습니다.

관련 문제