Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

c++ - Why might HttpOpenRequest fail with error 122?

The following code

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

is returning NULL with GetLastError() returning 122. A search suggests this error is

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

but gives no indication what buffer might be too small.

Which buffer might this relate to, and how can I make it bigger?

Update:

As has been pointed out, and detailed at http://support.microsoft.com/kb/208427, Internet Explorer, and presumably the wininet library, has a url limit of 2083 characters.

However looking at my url I find the url itself is around 40 characters. The 650k of data is in a name/value pair, for which wininet has no limit

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In general, your url should be 2k or less in size. Since you are performing a POST, you are heading in the right direction, its just that for the bulk of your data, you want to pass that as the body of the HTTP request like in this example:

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!

Cribbed from here: http://developers.sun.com/mobility/midp/ttips/HTTPPost/

Here's what I was thinking you would want to do:

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.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...