代码之家  ›  专栏  ›  技术社区  ›  David Sykes

为什么httpopenrequest会因错误122而失败?

  •  2
  • David Sykes  · 技术社区  · 14 年前

    以下代码

    fRequestHandle = HttpOpenRequestA(
                       fConnectHandle, 
                       "POST", url.c_str(), 
                       NULL, NULL, NULL,
                       INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE, 
                       0); 
    

    返回空值,GetLastError()返回122。搜索结果显示此错误为

    122 (ERROR_INSUFFICIENT_BUFFER) The data area passed to a system call is too small. 
    

    但没有指出哪种缓冲区可能太小。

    这可能与哪个缓冲区有关,如何使它变大?

    更新:

    如前所述,详情见 http://support.microsoft.com/kb/208427 ,Internet Explorer和Wininet库的URL限制为2083个字符。

    但是在查看我的URL时,我发现URL本身就在 40字 . 650K的数据位于名称/值对中,Wininet对此没有限制。

    3 回复  |  直到 9 年前
        1
  •  2
  •   reallynice Stefan Gehrig    9 年前

    一般来说,URL的大小应该小于或等于2K。因为您正在执行一个日志,所以您朝着正确的方向前进,对于大部分数据,您希望将其作为HTTP请求的主体传递,如本例中所示:

    POST /login.jsp HTTP/1.1
    Host: www.mysite.com
    User-Agent: Mozilla/4.0
    Content-Length: 27
    Content-Type: application/x-www-form-urlencoded
    
    userid=joe&password=guessme <--You need to do this!
    

    从这里开始: http://developers.sun.com/mobility/midp/ttips/HTTPPost/

    我想你应该做的是:

    std::string url("http://host.com/url");
    
    std::string dataPayload("name=value&othername=anothervalue");//Query string payload style.
    DWORD dataPayloadLength = dataPayload.length();
    
    std::ostringstream headerStream;
    headerStream << "content-length: ";
    headerStream << dataPayloadLength;
    std::string headers = headerStream.str();
    
    DWORD headerLength = headers.length();
    
    HINTERNET handle = HttpOpenRequest(hConnect,
        "POST",
        url.c_str(), 
        NULL, NULL, NULL,
        INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE, 
        0);
    
    if(!handle) {
        DWORD errorCode = GetLastError();
        //Handle error here.
    }
    
    //Use this thing to send POST values.
    if(! HttpSendRequest(handle,
        headers.c_str(),
        headerLength,
        dataPayload, //lpOptional <--Your POST data...not really optional for you.
        dataPayloadLength) {
    
        DWORD errorCode = GetLastError();
        //Handle error here.
    }
    
        2
  •  1
  •   James    14 年前
        3
  •  1
  •   OhadM    10 年前

    经过一些挖掘,当一个AV或防火墙阻止了我的GET请求时,我得到了错误122。