这次问题很大!很严重!附代码!!上传文件不成功,why?

职业炒股 2011-08-10 07:40:52
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的值------------------------------*/
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;

/*----------------------OOFA的转换成中文---------------------*/
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);



/*----------第三个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, _T("http://u.115.com"), NULL, INTERNET_FLAG_DONT_CACHE, NULL);
if (! hRequest3)
printf( "InternetRequest3 Error:%d ",GetLastError());


/*------------------------------本地文件的大小(第一种方法)-----------------------------------*/
//const TCHAR *filepath = _T("F:\\My DBank\\vs2010教程.doc");

/*HANDLE hFile = CreateFile( L"F:\\My DBank\\vs2010教程.doc", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
hFile = NULL;
printf("Can not open the file",GetLastError());
} */

const TCHAR * hFile = _T("F:\\My DBank\\115.txt");
LONGLONG iSize = GetFileSize(hFile);
printf("\rFileSize is%d", iSize );
/*------------------------组装包头,发送请求----------------------*/

string pHeader = ("Accept: text/*\r\nContent-Type: multipart/form-data; boundary=----------");
string strTemp = "";
randchar(strTemp);
pHeader.append(strTemp);
pHeader.append("\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中lpvBuffer的内容-----------------------------*/

string upBody = "------------";
upBody.append(strTemp);
upBody.append("\r\nContent-Disposition: form-data; name=\"Filename\"\r\n\r\n");
upBody.append("115.txt\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=\"115.txt\"\r\n");
upBody.append("Content-Type: application/octet-stream\r\n\r\n");


const char *pupBody=upBody.c_str();
int upBodyLen=static_cast <DWORD> (strlen(pupBody));


INTERNET_BUFFERSA BufferIn;
BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
BufferIn.Next = NULL;
BufferIn.lpcszHeader = const_cast<char *>(pHeader.c_str());
const char *pHeaderLen=pHeader.c_str();
BufferIn.dwHeadersLength = static_cast <DWORD> (strlen(pHeaderLen));
BufferIn.dwHeadersTotal = static_cast <DWORD> (strlen(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(!HttpSendRequestExA(hRequest3, &BufferIn, NULL, 0, 0))
{
printf( "Error on HttpSendRequestEx %d\n",GetLastError() );
return FALSE;
}

/*-------------------上传文件----------------------*/
const DWORD cBuffsize = 64*1024;
TCHAR* buffer = new TCHAR[cBuffsize];
DWORD dwRead;
printf( "\n n最大是%d",BufferIn.dwBufferTotal/cBuffsize );
for( int n=1; n<=(int)BufferIn.dwBufferTotal/1024+1; n++ )
{
if(!InternetWriteFile(hRequest3, buffer, cBuffsize, &dwRead))
{
printf("\nError on InternetWriteFile %d",GetLastError());
return false;
}
printf( "\r%d bytes sent.", n*1024);
}



/*--------------------------第四个GET包----------------------------------------*/

wstring hdrs4 = _T("Accept: */*\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\nConnection: Keep-Alive\r\nHost: u.115.com\r\nPragma: no-cache");

HINTERNET hSession4 = InternetOpen(_T("END"),INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

HINTERNET hConnect4 = InternetConnect(hSession4, _T("u.115.com"), 80, _T(""), _T(""), INTERNET_SERVICE_HTTP, NULL, NULL);

HINTERNET hRequest4 = HttpOpenRequest(hConnect4, _T("GET"),_T("?ct=file&ac=userfile&aid=1&cid=0&_t=1312870082439"), _T("HTTP/1.1"),_T("http://u.115.com/?ct=index&ac=my"), NULL ,INTERNET_FLAG_DONT_CACHE, NULL);
if(!hRequest4)
printf("HttpOpenRequest4 Error" , GetLastError());

BOOL hSendRequest4 = HttpSendRequest(hRequest4, hdrs4.c_str(),hdrs4.length(),NULL,NULL );
if(!hSendRequest4)
printf("HttpSendRequest4 Error" , GetLastError());

InternetCloseHandle(hRequest4);
InternetCloseHandle(hConnect4);
InternetCloseHandle(hSession4);

HttpEndRequest(hRequest3,NULL, 0, 0);
InternetCloseHandle(hRequest3);
InternetCloseHandle(hConnect3);
InternetCloseHandle(hSession3);

return 0;
}


此次运行后 会出现文件上传一部分文件, 然后 GetLastError()获得错误为12031 就是 网络链接重置
不知道这是为什么

我在HttpSendRequestExA 用的都不是宽字符 但是其他地方是宽字符,我在想是不是因为这个原因导致文件发送不成功,所以我在一楼的源码给出了,HttpSendRequestExW这个宽字符版本,但是现在更恶心的事情发生了, 就是HttpSendRequestExW竟然错误,错误代号87,就是参数不正确,看了无数遍,我的Buffin不可能有问题的,参数都符合条件。
现在我纠结了。。。。
...全文
301 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
职业炒股 2011-08-11
  • 打赏
  • 举报
回复
好吧 文件还没读进BUFFER
现在应该完善了。。。 头文件应该是宽字符的 包体的话就不用了 宽字符的会乱码的。

#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\\115.txt");
LONGLONG iSize = GetFileSize(hFile);
printf("\rFileSize is%d", iSize );

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

const DWORD Buffsize = 64*1024;
char buffer[Buffsize]={0};
DWORD dwBytesRead = 0;


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

if (!ReadFile(File, buffer, Buffsize-1, &dwBytesRead, NULL))
{
printf("ReadFile Error");
}


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


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中lpvBuffer的内容-----------------------------*/

string upBody = "------------";
upBody.append(strTemp);
upBody.append("\r\nContent-Disposition: form-data; name=\"Filename\"\r\n\r\n");
upBody.append("115.txt\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=\"115.txt\"\r\n");
upBody.append("Content-Type: application/octet-stream\r\n\r\n");

/* upBody.append("123456789");
upBody.append("\r\n------------");
upBody.append(strTemp);
upBody.append("\r\nContent-Disposition: form-data; name=\"Upload\"\r\n");
upBody.append("\r\nSubmit Query");
upBody.append("\r\n------------");
upBody.append(strTemp);
upBody.append("--");*/


const char *pupBody=upBody.c_str();
int upBodyLen=static_cast <DWORD> (strlen(pupBody));


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 = BufferIn.dwBufferLength; //这里是你要传送数据的总大小,比如一个文件的大小::GetFileSize(hFile, 0)

if(!HttpSendRequestExW(hRequest3, &BufferIn, NULL, 0, 0))
{
printf( "Error on HttpSendRequestEx %d\n",GetLastError() );
return FALSE;
}


/*------------------------------上传文件---------------------------------*/

DWORD dwRead = 0;

if(!InternetWriteFile(hRequest3, buffer, Buffsize, &dwRead))
{
printf("\nError on InternetWriteFile %d",GetLastError());
return false;
}
HttpEndRequest(hRequest3,NULL, 0, 0);
InternetCloseHandle(hRequest3);
InternetCloseHandle(hConnect3);
InternetCloseHandle(hSession3);

return 0;
}
职业炒股 2011-08-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 crazycrash 的回复:]
貌似你对Http协议很感兴趣,我以前也研究了一下。看着Window网络编程那本书学习的
我当时直接用的Socket类直接读取,需要自己解析字符串,只实现了读取html文档的功能,有点麻烦,也懒得搞了
[/Quote]

不是我很感兴趣。。是不得已不做啊。。。。。
TONY 2011-08-10
  • 打赏
  • 举报
回复
貌似你对Http协议很感兴趣,我以前也研究了一下。看着Window网络编程那本书学习的
我当时直接用的Socket类直接读取,需要自己解析字符串,只实现了读取html文档的功能,有点麻烦,也懒得搞了
  • 打赏
  • 举报
回复
1楼滴。。。。看不懂。。。。。。
职业炒股 2011-08-10
  • 打赏
  • 举报
回复
好吧 我承认我少发了 一个包。。。
唉 这个没有意义的帖子啊 !!!
明天再好好收拾我的程序!!!
下班。。。。。
职业炒股 2011-08-10
  • 打赏
  • 举报
回复
我去。。。一楼被占了。。。速度真快。。。。。
职业炒股 2011-08-10
  • 打赏
  • 举报
回复
这里给出 HttpSendRequestExA(hRequest3, &BufferIn, NULL, 0, 0)的宽字符版
最后一个发包可以忽视,没什么用的。
里面还有一些程序是我注释掉的其他方法。

#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的值------------------------------*/
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;

/*----------------------OOFA的转换成中文---------------------*/


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);



/*----------第三个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, _T("http://u.115.com"), NULL, INTERNET_FLAG_DONT_CACHE, NULL);
if (! hRequest3)
printf( "InternetRequest3 Error:%d ",GetLastError());


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

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

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中lpvBuffer的内容-----------------------------*/

wstring upBody = _T("------------");
upBody.append(wstrTemp);
upBody.append(_T("\r\nContent-Disposition: form-data; name=\"Filename\"\r\n\r\n"));
upBody.append(_T("115.txt\r\n"));
upBody.append(_T("------------"));
upBody.append(wstrTemp);
upBody.append(_T("\r\nContent-Disposition: form-data; name=\"aid\"\r\n\r\n"));
upBody.append(_T("1\r\n"));
upBody.append(_T("------------"));
upBody.append(wstrTemp);
upBody.append(_T("\r\nContent-Disposition: form-data; name=\"cookie\"\r\n\r\n"));
wstring wOOFA ;
AnsiToUnicode(UrlDecode(subOOFA),wOOFA);
upBody.append(wOOFA);
upBody.append(_T("\r\n------------"));
upBody.append(wstrTemp);
upBody.append(_T("\r\nContent-Disposition: form-data; name=\"Filedata\"; filename=\"115.txt\"\r\n"));
upBody.append(_T("Content-Type: application/octet-stream\r\n\r\n"));

const wchar_t *pupBody=upBody.c_str();
int upBodyLen=static_cast <DWORD> (wcslen(pupBody));

LPINTERNET_BUFFERSW BufferIn;
BufferIn = new INTERNET_BUFFERSW [sizeof(LPINTERNET_BUFFERSW)];

BufferIn->dwStructSize = sizeof(LPINTERNET_BUFFERSW);
BufferIn->Next = NULL;

BufferIn->lpcszHeader = 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<wchar_t *>(upBody.c_str());
const wchar_t *pBufferLen=upBody.c_str();
BufferIn->dwBufferLength = static_cast <DWORD> (wcslen(pBufferLen));
BufferIn->dwBufferTotal = BufferIn->dwBufferLength + static_cast <DWORD>(iSize); //这里是你要传送数据的总大小,比如一个文件的大小::GetFileSize(hFile, 0)

if(!HttpSendRequestExW(hRequest3, BufferIn, NULL, 0, 0)) // 为什么会出现参数错误??
{
printf( "Error on HttpSendRequestEx %d\n",GetLastError() );
return FALSE;
}

/*-------------------上传文件----------------------*/
const DWORD cBuffsize = 64*1024;
TCHAR* buffer = new TCHAR[cBuffsize];
DWORD dwRead;
printf( "\n n最大是%d",BufferIn->dwBufferTotal/cBuffsize );
for( int n=1; n<=(int)BufferIn->dwBufferTotal/1024+1; n++ )
{
if(!InternetWriteFile(hRequest3, buffer, cBuffsize, &dwRead))
{
printf("\nError on InternetWriteFile %d",GetLastError());
return false;
}
printf( "\r%d bytes sent.", n*1024);
}



/*--------------------------第四个GET包----------------------------------------*/

wstring hdrs4 = _T("Accept: */*\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\nConnection: Keep-Alive\r\nHost: u.115.com\r\nPragma: no-cache");

HINTERNET hSession4 = InternetOpen(_T("END"),INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

HINTERNET hConnect4 = InternetConnect(hSession4, _T("u.115.com"), 80, _T(""), _T(""), INTERNET_SERVICE_HTTP, NULL, NULL);

HINTERNET hRequest4 = HttpOpenRequest(hConnect4, _T("GET"),_T("?ct=file&ac=userfile&aid=1&cid=0&_t=1312870082439"), _T("HTTP/1.1"),_T("http://u.115.com/?ct=index&ac=my"), NULL ,INTERNET_FLAG_DONT_CACHE, NULL);
if(!hRequest4)
printf("HttpOpenRequest4 Error" , GetLastError());

BOOL hSendRequest4 = HttpSendRequest(hRequest4, hdrs4.c_str(),hdrs4.length(),NULL,NULL );
if(!hSendRequest4)
printf("HttpSendRequest4 Error" , GetLastError());

InternetCloseHandle(hRequest4);
InternetCloseHandle(hConnect4);
InternetCloseHandle(hSession4);

HttpEndRequest(hRequest3,NULL, 0, 0);
InternetCloseHandle(hRequest3);
InternetCloseHandle(hConnect3);
InternetCloseHandle(hSession3);

return 0;
}
larchio2011 2011-08-10
  • 打赏
  • 举报
回复
看不懂 也占楼

18,356

社区成员

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

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