#include "stdafx.h"#include <windows.h>
#include <wininet.h>
#include <stdio.h>void main(int argc, _TCHAR *argv[])
{
    if(argc<3) {
        printf("USAGE: %s [webserver] [username]\n", argv[0]);
        return;
    }    //  Open the internet connection
    HINTERNET hInternetSession = InternetOpen(
        "Wininet Client App", INTERNET_OPEN_TYPE_PRECONFIG,
        NULL, NULL, 0) ;
    HINTERNET hInternetConnect = InternetConnect(hInternetSession, argv[1],
        INTERNET_DEFAULT_HTTPS_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0);
    HINTERNET hHttpOpenRequest = HttpOpenRequest(hInternetConnect, "GET", "",
        HTTP_VERSION, "", NULL, 
        INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_KEEP_CONNECTION | 
        INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID, 0);    BOOL bRet;
    DWORD dwLastError = 0;    // Send the HTTP request
    bRet = HttpSendRequest(hHttpOpenRequest, NULL, 0, 0, 0);
    if (!bRet) dwLastError = GetLastError();    // Get the certificate information from the server if available.
    INTERNET_CERTIFICATE_INFO sInfo;
    DWORD length = sizeof(sInfo);
    InternetQueryOption(hHttpOpenRequest, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
        (LPVOID)&sInfo, &length);
    printf("The Issuer of the server certificate is:\n%s\n", sInfo.lpszIssuerInfo );
    printf("The Subject of the server certificate is:\n%s\n", sInfo.lpszSubjectInfo );    // Look up the local certicate store to get a certificate that matches the one of the server.
    PCCERT_CONTEXT pCertContext = NULL;
    HCERTSTORE hCertStore = CertOpenSystemStore(NULL, "MY");
    char pszIssuer[256] = "\0";
    char pszSubject[256] = "\0";
    BOOL bGotCert = FALSE;    while(pCertContext = CertEnumCertificatesInStore(hCertStore,pCertContext))
    {
printf("The following certificates are available:\n");
        CertGetNameString(pCertContext,CERT_NAME_SIMPLE_DISPLAY_TYPE, CERT_NAME_ISSUER_FLAG, NULL,
            pszIssuer, 256);
printf("Issuer: %s\n", pszIssuer);
        CertGetNameString(pCertContext,CERT_NAME_SIMPLE_DISPLAY_TYPE,0,NULL,
            pszSubject, 256);
printf("Subject: %s\n", pszSubject);
        if ((strcmp(pszSubject, argv[2]) == 0) && (strcmp(pszIssuer, sInfo.lpszIssuerInfo) == 0)) {
            bGotCert = TRUE;
printf("\nFound a certificate that matches the web server certificate.\n");
            break;
        }
    }    // If the web server requires a cert and we found a matching one, attach the cert to the request.
    if (( dwLastError == ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED ) && bGotCert)
    {
printf("Sending a HTTP request to the web server with the found certificate...\n");
        if (InternetSetOption(hHttpOpenRequest, INTERNET_OPTION_CLIENT_CERT_CONTEXT,
            (void*)pCertContext, sizeof(*pCertContext)) )
        {
            CertDuplicateCertificateContext(pCertContext); 
        }
    }    bRet = HttpSendRequest(hHttpOpenRequest, NULL, 0, 0, 0);    if (bRet) {
        DWORD dwCode, dwSize;
        dwSize = sizeof(dwCode);
        if ( !HttpQueryInfo (hHttpOpenRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwCode, &dwSize, NULL))
        {
        }
        
        if (dwCode == 200)
            printf("Request sent to the server successfully.(HTTP Status: %d)\n", dwCode);
        if (dwCode == 403)
            printf("You didn't select a valid certificate. (HTTP Status: %d)\n", dwCode);
    } else {
        printf("Untrusted CA.\n");
    }    CertFreeCertificateContext(pCertContext);
    CertCloseStore(hCertStore,0);    InternetCloseHandle (hInternetSession);
    InternetCloseHandle (hInternetConnect);
    InternetCloseHandle (hHttpOpenRequest);
    
    printf("Press any key to continue...");
    getchar();
}能不能帮说明一下上面的代码呢 
我都看不到他请求的地址是什么 
然后我要怎么个请求法 
以前做快钱 ebuy的时候都是用WebService的 
但这个工行的说明文档就是看不懂 谢谢 

解决方案 »

  1.   

    HINTERNET hInternetConnect = InternetConnect(hInternetSession, argv[1])
    argv[1]就是url
    如果你不急,等下午有空帮你写个。
      

  2.   

    HINTERNET hInternetConnect = InternetConnect(hInternetSession, argv[1],
            INTERNET_DEFAULT_HTTPS_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0);
    这行代码应该是,
    argv[1]应该是请求的的地址
      

  3.   

    地址是通过参数传进去的:if(argc<3) {
            printf("USAGE: %s [webserver] [username]\n", argv[0]);
            return;
        }
      

  4.   

    这段代码使用Http协议发送请求,并读取返回值.
    C#使用http可以参考:
    HTTP协议编程 C# 
    http://info.52z.com/html/23853.html