C++ NPAPI WinHttpConnect 失败

发布于 2024-11-29 20:19:11 字数 8641 浏览 0 评论 0原文

现在我知道问题是 hConnect 未通过调试器成功初始化,我想知道为什么,感谢

它的 javascript 代码调用者

t=function(){
    var type = "GET",
    host = "183.60.139.201",
    port = 80,
    uri = "/",
    useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)",
    headers = "";
    getdata = "",
    postdata = "";

    resolveTimeout = 10000;
    connectTimeout = 10000;
    sendTimeout = 10000;
    receiveTimeout = 10000;

    return test.http( type 
                      ,host
                      ,port
                      ,uri
                      ,useragent
                      ,headers
                      ,getdata
                      ,postdata
                      ,resolveTimeout
                      ,connectTimeout
                      ,sendTimeout
                      ,receiveTimeout
                    );
} 
t(); 

及其 C++ 源代码

#include "plugin.h"
#include "simplehttp.h"

#pragma comment(lib, "winhttp.lib")
#include <windows.h>
#include <winhttp.h>
#include <string>

std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

using namespace std;

bool HttpRequest(ScriptablePluginObject* obj, const NPVariant* args,
                unsigned int argCount, NPVariant* result){

    string type = "GET";
    string host = "";
    unsigned int port = INTERNET_DEFAULT_HTTP_PORT;
    string uri ="/";
    string useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)";
    string additionalheaders = "";
    string getdata = "";
    string postdata = "";
    __asm {
        int 3
    }
    unsigned int resolveTimeout = 10000;
    unsigned int connectTimeout = 10000;
    unsigned int sendTimeout = 10000;
    unsigned int receiveTimeout = 10000;

    for( int i=0;i<argCount;i++){
        NPVariant arg = args[i];
        switch (i){
            case 0:
                type = arg.value.stringValue.UTF8Characters;
                break;
            case 1:
                host = arg.value.stringValue.UTF8Characters;
                break;
            case 2:
                port = arg.value.intValue;
                break;
            case 3:
                uri = arg.value.stringValue.UTF8Characters;
                break;
            case 4:
                useragent = arg.value.stringValue.UTF8Characters;
                break;
            case 5:
                additionalheaders = arg.value.stringValue.UTF8Characters;
                break;
            case 6:
                getdata = arg.value.stringValue.UTF8Characters;
                break;
            case 7:
                postdata = arg.value.stringValue.UTF8Characters;
                break;
            case 8:
                resolveTimeout = arg.value.intValue;
                break;
            case 9:
                connectTimeout = arg.value.intValue;
                break;
            case 10:
                sendTimeout = arg.value.intValue;
                break;
            case 11:
                receiveTimeout = arg.value.intValue;
                break;
        }
    }
    BOOL usePost = (type == "POST" && postdata != "");

    DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    BOOL bResults = FALSE;
    string page = "";

    HINTERNET hSession = NULL,
              hConnect = NULL,
              hRequest = NULL;

    hSession = WinHttpOpen( s2ws(useragent).c_str(),
                            WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                            WINHTTP_NO_PROXY_NAME,
                            WINHTTP_NO_PROXY_BYPASS,
                            0);
    if (!WinHttpSetTimeouts( hSession, resolveTimeout, connectTimeout, sendTimeout, receiveTimeout)){
        goto endHttpRequest;
    };

    if(hSession){
        LPCWSTR tes = s2ws(host).c_str();
        hConnect = WinHttpConnect( hSession,
                                            tes,
                                            port,
                                            0);
    }else{
        goto endHttpRequest;
    }

    if(hConnect){ // FAILED HERE!!!!
        if(!usePost){
            hRequest = WinHttpOpenRequest( hConnect,
                                       s2ws(type).c_str(),
                                       s2ws(uri.append(getdata)).c_str(),
                                       NULL,
                                       WINHTTP_NO_REFERER,
                                       WINHTTP_DEFAULT_ACCEPT_TYPES,
                                       WINHTTP_FLAG_REFRESH);
        }else{
            hRequest = WinHttpOpenRequest( hConnect,
                                       s2ws(type).c_str(),
                                       s2ws(uri).c_str(),
                                       NULL,
                                       WINHTTP_NO_REFERER,
                                       WINHTTP_DEFAULT_ACCEPT_TYPES,
                                       WINHTTP_FLAG_REFRESH);
        }

    }else{
        goto endHttpRequest;
    }

    if(hRequest)
        bResults = WinHttpAddRequestHeaders( hRequest, 
                                                      L"Cookie:", 
                                                      -1, 
                                                      WINHTTP_ADDREQ_FLAG_REPLACE);
    if(hRequest)
        bResults = WinHttpAddRequestHeaders( hRequest,
                                                      s2ws(additionalheaders).c_str(),
                                                      -1,
                                                      WINHTTP_ADDREQ_FLAG_ADD);
    if(hRequest)
    {
        if(usePost){
            bResults = WinHttpAddRequestHeaders( hRequest,
                                                      L"Content-Type: application/x-www-form-urlencoded",
                                                      -1,
                                                      WINHTTP_ADDREQ_FLAG_ADD);
            bResults = WinHttpAddRequestHeaders( hRequest,
                                                      L"Content-Length: "+ postdata.size(),
                                                      -1,
                                                      WINHTTP_ADDREQ_FLAG_ADD);
            bResults = WinHttpSendRequest( hRequest,
                                                WINHTTP_NO_ADDITIONAL_HEADERS,
                                                0,
                                                (LPVOID)postdata.c_str(),
                                                postdata.size(),
                                                postdata.size(),
                                                0);
        }else{
            bResults = WinHttpSendRequest( hRequest,
                                                WINHTTP_NO_ADDITIONAL_HEADERS,
                                                0,
                                                WINHTTP_NO_REQUEST_DATA,
                                                0,
                                                0,
                                                0);
        }
    }

    if(bResults){
        bResults = WinHttpReceiveResponse(hRequest,NULL);
    }else{
        goto endHttpRequest;
    }

    if(bResults){
        do{
            dwSize = 0;
            if(!WinHttpQueryDataAvailable( hRequest,&dwSize)){
                return false;
            }

            pszOutBuffer = new char[dwSize+1];

            if(!pszOutBuffer){
                dwSize = 0;
                return false;
            }else{
                ZeroMemory(pszOutBuffer,dwSize+1);

                if(!WinHttpReadData( hRequest,
                                     (LPVOID)pszOutBuffer,
                                     dwSize,
                                     &dwDownloaded)){
                    return false;
                }else{
                    page.append(pszOutBuffer);
                }
                delete [] pszOutBuffer;
            }
        }while(dwSize>0);
    }

    endHttpRequest:
    char* npOutString = (char *)npnfuncs->memalloc(page.size()+1);
    strcpy(npOutString,page.c_str());
    STRINGZ_TO_NPVARIANT(npOutString,*result);
    if( hRequest ) WinHttpCloseHandle( hRequest );
    if( hConnect ) WinHttpCloseHandle( hConnect );
    if( hSession ) WinHttpCloseHandle( hSession );
    return true;
};

Now i know the problem is hConnect not successfully initialize through the debugger, and i wonder why,thanks

its the javascript code caller

t=function(){
    var type = "GET",
    host = "183.60.139.201",
    port = 80,
    uri = "/",
    useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)",
    headers = "";
    getdata = "",
    postdata = "";

    resolveTimeout = 10000;
    connectTimeout = 10000;
    sendTimeout = 10000;
    receiveTimeout = 10000;

    return test.http( type 
                      ,host
                      ,port
                      ,uri
                      ,useragent
                      ,headers
                      ,getdata
                      ,postdata
                      ,resolveTimeout
                      ,connectTimeout
                      ,sendTimeout
                      ,receiveTimeout
                    );
} 
t(); 

its C++ source

#include "plugin.h"
#include "simplehttp.h"

#pragma comment(lib, "winhttp.lib")
#include <windows.h>
#include <winhttp.h>
#include <string>

std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

using namespace std;

bool HttpRequest(ScriptablePluginObject* obj, const NPVariant* args,
                unsigned int argCount, NPVariant* result){

    string type = "GET";
    string host = "";
    unsigned int port = INTERNET_DEFAULT_HTTP_PORT;
    string uri ="/";
    string useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)";
    string additionalheaders = "";
    string getdata = "";
    string postdata = "";
    __asm {
        int 3
    }
    unsigned int resolveTimeout = 10000;
    unsigned int connectTimeout = 10000;
    unsigned int sendTimeout = 10000;
    unsigned int receiveTimeout = 10000;

    for( int i=0;i<argCount;i++){
        NPVariant arg = args[i];
        switch (i){
            case 0:
                type = arg.value.stringValue.UTF8Characters;
                break;
            case 1:
                host = arg.value.stringValue.UTF8Characters;
                break;
            case 2:
                port = arg.value.intValue;
                break;
            case 3:
                uri = arg.value.stringValue.UTF8Characters;
                break;
            case 4:
                useragent = arg.value.stringValue.UTF8Characters;
                break;
            case 5:
                additionalheaders = arg.value.stringValue.UTF8Characters;
                break;
            case 6:
                getdata = arg.value.stringValue.UTF8Characters;
                break;
            case 7:
                postdata = arg.value.stringValue.UTF8Characters;
                break;
            case 8:
                resolveTimeout = arg.value.intValue;
                break;
            case 9:
                connectTimeout = arg.value.intValue;
                break;
            case 10:
                sendTimeout = arg.value.intValue;
                break;
            case 11:
                receiveTimeout = arg.value.intValue;
                break;
        }
    }
    BOOL usePost = (type == "POST" && postdata != "");

    DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    BOOL bResults = FALSE;
    string page = "";

    HINTERNET hSession = NULL,
              hConnect = NULL,
              hRequest = NULL;

    hSession = WinHttpOpen( s2ws(useragent).c_str(),
                            WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                            WINHTTP_NO_PROXY_NAME,
                            WINHTTP_NO_PROXY_BYPASS,
                            0);
    if (!WinHttpSetTimeouts( hSession, resolveTimeout, connectTimeout, sendTimeout, receiveTimeout)){
        goto endHttpRequest;
    };

    if(hSession){
        LPCWSTR tes = s2ws(host).c_str();
        hConnect = WinHttpConnect( hSession,
                                            tes,
                                            port,
                                            0);
    }else{
        goto endHttpRequest;
    }

    if(hConnect){ // FAILED HERE!!!!
        if(!usePost){
            hRequest = WinHttpOpenRequest( hConnect,
                                       s2ws(type).c_str(),
                                       s2ws(uri.append(getdata)).c_str(),
                                       NULL,
                                       WINHTTP_NO_REFERER,
                                       WINHTTP_DEFAULT_ACCEPT_TYPES,
                                       WINHTTP_FLAG_REFRESH);
        }else{
            hRequest = WinHttpOpenRequest( hConnect,
                                       s2ws(type).c_str(),
                                       s2ws(uri).c_str(),
                                       NULL,
                                       WINHTTP_NO_REFERER,
                                       WINHTTP_DEFAULT_ACCEPT_TYPES,
                                       WINHTTP_FLAG_REFRESH);
        }

    }else{
        goto endHttpRequest;
    }

    if(hRequest)
        bResults = WinHttpAddRequestHeaders( hRequest, 
                                                      L"Cookie:", 
                                                      -1, 
                                                      WINHTTP_ADDREQ_FLAG_REPLACE);
    if(hRequest)
        bResults = WinHttpAddRequestHeaders( hRequest,
                                                      s2ws(additionalheaders).c_str(),
                                                      -1,
                                                      WINHTTP_ADDREQ_FLAG_ADD);
    if(hRequest)
    {
        if(usePost){
            bResults = WinHttpAddRequestHeaders( hRequest,
                                                      L"Content-Type: application/x-www-form-urlencoded",
                                                      -1,
                                                      WINHTTP_ADDREQ_FLAG_ADD);
            bResults = WinHttpAddRequestHeaders( hRequest,
                                                      L"Content-Length: "+ postdata.size(),
                                                      -1,
                                                      WINHTTP_ADDREQ_FLAG_ADD);
            bResults = WinHttpSendRequest( hRequest,
                                                WINHTTP_NO_ADDITIONAL_HEADERS,
                                                0,
                                                (LPVOID)postdata.c_str(),
                                                postdata.size(),
                                                postdata.size(),
                                                0);
        }else{
            bResults = WinHttpSendRequest( hRequest,
                                                WINHTTP_NO_ADDITIONAL_HEADERS,
                                                0,
                                                WINHTTP_NO_REQUEST_DATA,
                                                0,
                                                0,
                                                0);
        }
    }

    if(bResults){
        bResults = WinHttpReceiveResponse(hRequest,NULL);
    }else{
        goto endHttpRequest;
    }

    if(bResults){
        do{
            dwSize = 0;
            if(!WinHttpQueryDataAvailable( hRequest,&dwSize)){
                return false;
            }

            pszOutBuffer = new char[dwSize+1];

            if(!pszOutBuffer){
                dwSize = 0;
                return false;
            }else{
                ZeroMemory(pszOutBuffer,dwSize+1);

                if(!WinHttpReadData( hRequest,
                                     (LPVOID)pszOutBuffer,
                                     dwSize,
                                     &dwDownloaded)){
                    return false;
                }else{
                    page.append(pszOutBuffer);
                }
                delete [] pszOutBuffer;
            }
        }while(dwSize>0);
    }

    endHttpRequest:
    char* npOutString = (char *)npnfuncs->memalloc(page.size()+1);
    strcpy(npOutString,page.c_str());
    STRINGZ_TO_NPVARIANT(npOutString,*result);
    if( hRequest ) WinHttpCloseHandle( hRequest );
    if( hConnect ) WinHttpCloseHandle( hConnect );
    if( hSession ) WinHttpCloseHandle( hSession );
    return true;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好菇凉咱不稀罕他 2024-12-06 20:19:11

我在 python 中看到了这个错误,解决方案是以 utf-16 编码传递 url 字符串。

I've seen this error in python, the solution was to pass the url string in utf-16 encoding.

倾`听者〃 2024-12-06 20:19:11

尝试使用 wstring uri;
宽版本则不需要转换它。

try using wstring uri;
The wide version then you don't need to convert it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文