Why might HttpOpenRequest fail with error 122?

后端 未结 3 1158
轻奢々
轻奢々 2020-12-20 02:52

The following code

fRequestHandle = HttpOpenRequestA(
                   fConnectHandle, 
                   \"POST\", url.c_str(), 
                   NULL,         


        
3条回答
  •  死守一世寂寞
    2020-12-20 03:32

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

提交回复
热议问题