双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

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

职业炒股 职业炒股 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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不可能有问题的,参数都符合条件。
现在我纠结了。。。。
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
职业炒股
网络编程 18,356 成员 64,161 内容