服务端怎么通过ASP处理客户端发来的文件?

ailink000 2011-08-12 09:03:01
我有这么一个需求,想利用Wininet http程序将文件上传到服务器,服务器利用ASP来处理。在实现上我采用的将文件全部转换成十六进制格式,然后以http://xxx.com?A=file&B=00FFAABBCCDD方式提交给ASP处理,但是带来的问题就是速度慢,因为我将文件转换成十六进制字符的形式,就相当于将文件的体积增大了二倍。 我的问题是有什么别的方法吗? 有人说用二进制数据流的方式来发送接收,二进制数据流是怎么一回事?怎么讲文件转成二进制数据流?哪位高手会的话给我讲讲,简单的提供几行代码看看,有例子更好,不胜感谢,问题解决分数大大的有
...全文
104 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
向立天 2011-08-12
  • 打赏
  • 举报
回复
文件本身就是二进制数据流
WCT511 2011-08-12
  • 打赏
  • 举报
回复
要抓包分析包的。。。我也是新手。。现在在做下载。。


有一个问题要请教
上传几百兆的文件
如何把文件读取然后上传??? 不要分片的、。。。
WCT511 2011-08-12
  • 打赏
  • 举报
回复
/*------------------------------上传文件---------------------------------*/

DWORD dwRead = 0;

if(!InternetWriteFile(hRequest3, buffer, iSize+1, &dwRead))
{
printf("\nError on InternetWriteFile %d",GetLastError());
return false;
}

if(!HttpEndRequest(hRequest3, NULL, 0, 0))
{
printf( "\nError on HttpEndRequest %lu \n ", GetLastError());
return FALSE;
}
InternetCloseHandle(hRequest3);
InternetCloseHandle(hConnect3);
InternetCloseHandle(hSession3);
return true;
}

WCT511 2011-08-12
  • 打赏
  • 举报
回复
好吧 楼主看我的代码
读文件 用 readfile直接出来就是二进制。。
我的是wininet实现登录上传 已经实现了 你可以参考一下
tool.h是我自己的东西 只要是编码转换 不公开
#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
#include <iostream>
#include "tool.h"
using namespace std;

#pragma comment(lib, "Wininet.lib")

int main(int argc, char* argv[])
{
/*--------------------------------第一个POST包--------------------------------------*/
wstring hdrs = _T("Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\nReferer: http://passport.115.com/ \r\nAccept-Language: zh-CN\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; aff-kingsoft-ciba; .NET4.0C; .NET4.0E)\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip, deflate\r\nHost: passport.115.com\r\nContent-Length: 92\r\nConnection: Keep-Alive\r\nPragma: no-cache\r\n");
string body = "login%5Baccount%5D=wct511@126.com&login%5Bpasswd%5D=wctyws511629&goto=http%3A%2F%2Fu.115.com";
const char *pBody=body.c_str();
int bodyLen=static_cast <DWORD> (strlen(pBody));

HINTERNET hSession1 = InternetOpen(_T("Load"),INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

HINTERNET hConnect1 = InternetConnect(hSession1, _T("passport.115.com"), 80, _T(""), _T(""), INTERNET_SERVICE_HTTP, NULL, NULL);

HINTERNET hRequest1 = HttpOpenRequest(hConnect1, _T("POST"), _T("/?ac=login"), HTTP_VERSION, _T("http://passport.115.com/"), NULL ,INTERNET_FLAG_DONT_CACHE, NULL);
if(!hRequest1)
printf("HttpOpenRequest1 Error" , GetLastError());

BOOL hSendRequest = HttpSendRequest(hRequest1, hdrs.c_str(),hdrs.length(),(LPVOID)(pBody),bodyLen );
if(!hSendRequest)
printf("HttpSendRequest1 Error" , GetLastError());

/*--------------------------Cookie分离出OOFA的值(Decode)------------------------------*/
char szURL[256]= ("http://passport.115.com/?ac=login");
char * lpszData = NULL; //存放cookie
wstring wCookie;
string strCookie;
string subOOFA;
DWORD dwSize=5000; //
InternetGetCookieA(szURL ,NULL,lpszData, &dwSize);
if (InternetGetCookieA(("http://passport.115.com/?ac=login"), NULL, lpszData, &dwSize))
{
lpszData=new char [dwSize+1];
::memset(lpszData,0,dwSize+1);
}
if (InternetGetCookieA(("http://passport.115.com/?ac=login"), NULL, lpszData, &dwSize))
{

Utf8ToUnicode(lpszData,wCookie); //char to wstring
UnicodeToAnsi(wCookie,strCookie);//wstring to string
int indexCh1 = strCookie.find("OOFA",1);
string subCh1= strCookie.substr(indexCh1); //OOFA=
subOOFA = subCh1.substr(5);
printf("Getting OOFA succeed\n");
}
delete lpszData;
lpszData = NULL;

InternetCloseHandle(hRequest1);
InternetCloseHandle(hConnect1);
InternetCloseHandle(hSession1);


/*--------------------------第二个GET包----------------------------------------*/

std::wstring hdrs2 = _T("Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\nAccept-Language: zh-CN\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; aff-kingsoft-ciba; .NET4.0C; .NET4.0E)\r\nAccept-Encoding: gzip, deflate\r\nHost: u.115.com\r\n\r\n");

HINTERNET hSession2 = InternetOpen(_T("UPLOAD2"),INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

HINTERNET hConnect2 = InternetConnect(hSession2, _T("u.115.com"), 80, _T(""), _T(""), INTERNET_SERVICE_HTTP, NULL, NULL); //建立连接

HINTERNET hRequest2 = HttpOpenRequest(hConnect2, _T("GET"),NULL, HTTP_VERSION, NULL, NULL ,INTERNET_FLAG_DONT_CACHE, NULL);
if(!hRequest2)
printf("HttpOpenRequest2 Error" , GetLastError());

BOOL hSendRequest2 = HttpSendRequest(hRequest2, hdrs2.c_str(),hdrs2.length(),NULL,NULL );
if(!hSendRequest2)
printf("HttpSendRequest2 Error" , GetLastError());

InternetCloseHandle(hRequest2);
InternetCloseHandle(hConnect2);
InternetCloseHandle(hSession2);


/*-------------------------------------第三个get包 获得CUS_2364079=1;--------------------------------------*/
wstring hdrs21 = _T("Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\nAccept-Language: zh-CN\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; aff-kingsoft-ciba; .NET4.0C; .NET4.0E)\r\nAccept-Encoding: gzip, deflate\r\nHost: u.115.com\r\nConnection: Keep-Alive\r\n\r\n");

HINTERNET hSession21 = InternetOpen(_T("GETCOOKIE2"),INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

HINTERNET hConnect21 = InternetConnect(hSession21, _T("u.115.com"), 80, _T(""), _T(""), INTERNET_SERVICE_HTTP, NULL, NULL); //建立连接

HINTERNET hRequest21 = HttpOpenRequest(hConnect21, _T("GET"),_T("/?ct=frame") , HTTP_VERSION, _T("http://u.115.com/?ct=index&ac=my"), NULL ,INTERNET_FLAG_DONT_CACHE, NULL);
if(!hRequest21)
printf("HttpOpenRequest21 Error" , GetLastError());

BOOL hSendRequest21 = HttpSendRequest(hRequest21, hdrs21.c_str(),hdrs21.length(),NULL,NULL );
if(!hSendRequest21)
printf("HttpSendRequest21 Error" , GetLastError());

InternetCloseHandle(hRequest21);
InternetCloseHandle(hConnect21);
InternetCloseHandle(hSession21);


/*----------第四个POST包 UPLOAD-----------*/

HINTERNET hSession3 = InternetOpen( _T("Upload file"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if (!hSession3)
printf( "InternetOpen3 Error:%d ",GetLastError());

HINTERNET hConnect3 = InternetConnect(hSession3, _T("up.u.115.com"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (! hConnect3 )
printf( "InterneConnect3 Error:%d ",GetLastError());

HINTERNET hRequest3 = HttpOpenRequest(hConnect3 , _T("POST"),_T("/upload"), HTTP_VERSION, NULL, NULL, INTERNET_FLAG_DONT_CACHE, NULL);
if (! hRequest3)
printf( "InternetRequest3 Error:%d ",GetLastError());
/*-----------------------获取文件大小(第三种方法)----------------------------*/

const TCHAR * hFile = _T("F:\\My DBank\\EmailSetting.exe");
size_t iSize = GetFileSize(hFile);
printf("\rFileSize is %d", iSize );

/*----------------------------------读文件到buffer-------------------------------------*/
wchar_t* FileName =_T("F:\\My DBank\\EmailSetting.exe");

// const DWORD Buffsize =64*1024;
char *buffer=new char [iSize+1];
memset(buffer,0,iSize+1);
DWORD dwBytesRead = 0;

HANDLE File = CreateFile (FileName,GENERIC_READ, FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL );

if (!ReadFile(File, buffer, iSize, &dwBytesRead, NULL))
{
printf("ReadFile Error!ErrorCode is",GetLastError());
}

/*------------------------组装上传包包头,发送请求----------------------*/


wstring pHeader = _T("Accept: text/*\r\nContent-Type: multipart/form-data; boundary=----------");
string strTemp;
randchar(strTemp);
wstring wstrTemp ;
AnsiToUnicode(strTemp,wstrTemp);
pHeader.append(wstrTemp);
pHeader.append(_T("\r\nUser-Agent: Shockwave Flash\r\nHost: up.u.115.com\r\nConnection: Keep-Alive\r\nPragma: no-cache\r\n\r\n"));

/*------------------------INTERNET_BUFFERSA BufferIn中 Buffer的内容-----------------------------*/

string upBody = "------------";
upBody.append(strTemp);
upBody.append("\r\nContent-Disposition: form-data; name=\"Filename\"\r\n\r\n");
upBody.append("EmailSetting.exe\r\n");
upBody.append("------------");
upBody.append(strTemp);
upBody.append("\r\nContent-Disposition: form-data; name=\"aid\"\r\n\r\n");
upBody.append("1\r\n");
upBody.append("------------");
upBody.append(strTemp);
upBody.append("\r\nContent-Disposition: form-data; name=\"cookie\"\r\n\r\n");
upBody.append(UrlDecode(subOOFA));
upBody.append("\r\n------------");
upBody.append(strTemp);
upBody.append("\r\nContent-Disposition: form-data; name=\"Filedata\"; filename=\"EmailSetting.exe\"\r\n");
upBody.append("Content-Type: application/octet-stream\r\n\r\n");
upBody.append ("------------");
upBody.append (strTemp);
upBody.append("\r\nContent-Disposition: form-data; name=\"Upload\"\r\n");
upBody.append("Submit Query\r\n------------");
upBody.append(strTemp);
upBody.append("--");

INTERNET_BUFFERSW BufferIn;

BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
BufferIn.Next =NULL;
BufferIn.lpcszHeader = const_cast<wchar_t *>(pHeader.c_str());
const wchar_t *pHeaderLen=pHeader.c_str();
BufferIn.dwHeadersLength = static_cast <DWORD> (wcslen(pHeaderLen));
BufferIn.dwHeadersTotal = static_cast <DWORD> (wcslen(pHeaderLen));
BufferIn.lpvBuffer = const_cast<char *>(upBody.c_str());
const char *pBufferLen=upBody.c_str();
BufferIn.dwBufferLength = static_cast <DWORD> (strlen(pBufferLen));
BufferIn.dwBufferTotal = iSize+ BufferIn.dwBufferLength ; //这里是你要传送数据的总大小,比如一个文件的大小::GetFileSize(hFile, 0)


if(!HttpSendRequestExW(hRequest3, &BufferIn, NULL, 0, 0))
{
printf( "Error on HttpSendRequestEx %d\n",GetLastError() );
return FALSE;
}
ailink000 2011-08-12
  • 打赏
  • 举报
回复
顶起啊,咋没人指导呢
ailink000 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wct511 的回复:]
要抓包分析包的。。。我也是新手。。现在在做下载。。


有一个问题要请教
上传几百兆的文件
如何把文件读取然后上传??? 不要分片的、。。。
[/Quote]
分什么片? 自己设置的大小,比如每次读8K写8K不就行了

18,357

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧