asp.net - 使用httpSendRequest c++上传文件

标签 asp.net c winapi http wininet

我正在尝试通过 POST 请求(c++ 和 winapi)将文件发送到 HTTP 服务器,步骤:

// Read file into "buff" and file size into "buffSize" 
        ....
    ....
    ....

    HINTERNET internetRoot;
    HINTERNET httpSession;
    HINTERNET httpRequest;

    internetRoot = InternetOpen(agent_info, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);

    //Connecting to the http server
    httpSession = InternetConnect(internetRoot, IP,PORT_NUMBER, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, NULL);

    //Creating a new HTTP POST request to the default resource on the server
    httpRequest = HttpOpenRequest(httpSession, TEXT("POST"), TEXT("/Post.aspx"), NULL, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, NULL);

    //Send POST request
    HttpSendRequest(httpRequest, NULL, NULL, buff, buffSize);

    //Closing handles...

在服务器中,我正在使用此代码 (asp.net) 接收文件

Stream httpStream;
        try
        {
            httpStream = request.RequestContext.HttpContext.Request.InputStream;
        }
        catch (HttpException)
        {
            return;
        }

            byte[] tmp = new byte[httpStream.Length];
            int bytesRead = httpStream.Read(tmp, 0, 1024 * 1024);
            int totalBytesRead = bytesRead;
            while (bytesRead > 0)
            {
                bytesRead = httpStream.Read(tmp, totalBytesRead, 1024 * 1024);
                totalBytesRead += bytesRead;
            }
            httpStream.Close();
            httpStream.Dispose();

           //Save "tmp" to file...

我可以在本地服务器(visual studio asp 服务器)上发送大文件,但无法将超过 1 MB 的文件发送到 Internet 服务器。 (HttpOpenRequest 失败) 有没有更好的上传文件的方法?

最佳答案

警告:这些天我的 Wininet 生锈了。

我想知道您是否应该自己设置“Content-Length” header 。您的代码似乎假设 a) 您正在发出 HTTP/1.0 请求或 b) HttpSendRequest 将为您添加 header (我认为不会)。

无论哪种方式,如果服务器无法快速确定请求大小,IIS 的默认配置都会在不告知服务器传入请求有多大的情况下拒绝它。

我的猜测是,如果您使用 HttpSendRequest 函数的 lpszHeadersdwHeadersLength 参数来包含适当的“Content-Length” header 问题将得到解决。

关于asp.net - 使用httpSendRequest c++上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10130880/

相关文章:

winapi - SW_SHOW和SW_SHOWNORMAL的含义

asp.net - 从 ASP.NET 在 Facebook 中将 div 内容共享为图像

c# - 在文本框中输入字符的击键?

c# - 为什么 Request.QueryString ["key"] 停止工作?

c - 为什么 fopen 参数限制在 C 标准和 <stdio.h> 头文件中限定?

c++ - C/C++ : How to convert 6bit ASCII to 7bit ASCII

c - maloc 是如何设法获取已在 SSL Heartbeat 中分配的内存的?

windows - 使用 dpiawareness=1 时辅助监视器的坐标很奇怪

css - 多列在使用 ASP.NET 的 IE11 中不起作用

asp.net - 为什么我的 asp.net 页面会被同步访问?