Wininet没有向服务器发送cookie

时间:2014-03-18 15:30:16

标签: c cookies wininet mingw32

我的应用程序中有以下代码。 在HttpSendRequest()中,我放了一个包含几个选项的标题字符串,但是cookie部分被删除了。

如何通过wininet避免这种行为。 ?

char *get_data_from_url_new(gchar *data_url)
{
    char Agent[128] = {0};
    char ServerName[128] = {0};
    char Verb[128] = {0};
    char ObjectName[128] = {0};
    char Optional[128] = {0};
    int OptionalLength = 0;
    char Headers[128] = {0};
    int HeadersLength = 0;
    HINTERNET InetOpen;
    HINTERNET InetConnect;
    HINTERNET HtOpenRequest;
    DWORD flags = 0;

    if (!InternetGetConnectedState(&flags, 0)) {
        return "0";
    }

    sprintf(Agent, "%s", "myagent");
    InetOpen = InternetOpen(Agent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (InetOpen == NULL) {
        return "0";
    }

    sprintf(ServerName, "%s", "192.168.2.150");
    InetConnect = InternetConnect(InetOpen, ServerName, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    if (InetConnect == NULL) {
        return "0";
    }

    sprintf(Verb, "%s", "POST");
    sprintf(ObjectName, "%s", "cgi-bin/icetea?ifr=101");
    HtOpenRequest = HttpOpenRequest(InetConnect, Verb, ObjectName, NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
    if (HtOpenRequest == NULL) {
        return "0";
    }

    sprintf(Headers, "%s", "Connection: keep-alive\r\nCookie: username=johndoe\r\nContent-Type: application/x-www-form-urlencoded");
    HeadersLength = strlen(Headers);
    sprintf(Optional, "%s", "Some FORM data in pairs");
    OptionalLength = strlen(Optional);

    HttpSendRequest(HtOpenRequest, Headers, HeadersLength, Optional, OptionalLength);

    InternetCloseHandle(InetOpen);
    InternetCloseHandle(InetConnect);
    InternetCloseHandle(HtOpenRequest); 

    return "0";
}

1 个答案:

答案 0 :(得分:1)

尝试在INTERNET_FLAG_NO_COOKIES调用中添加HttpOpenRequest标志,以禁用wininet中的自动Cookie管理。