Http请求

根据HTTP标准,HTTP请求可以使用多种请求方法。

HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法。

HTTP1.1新增了五种请求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。

1GET请求指定的页面信息,并返回实体主体。
2HEAD类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头
3POST向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。
4PUT从客户端向服务器传送的数据取代指定的文档的内容。
5DELETE请求服务器删除指定的页面。
6CONNECTHTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。
7OPTIONS允许客户端查看服务器的性能。
8TRACE回显服务器收到的请求,主要用于测试或诊断。

http请求

10-23

int CommonUtil::HttpSendRequest(wstring wstrURL,rn wstring wstrVerb,rn LPVOID lpOptional,rn DWORD dwOptionalLength,rn string &strResult)rn strResult="";rn DWORD dwSize = 0;rn DWORD dwFlags = 0;rn std::wstring strServer;rn std::wstring strObject;rn INTERNET_PORT nPort;rn INT nScheme;rn BOOL bHttps = FALSE;rn BOOL bResult = FALSE;rnrn HINTERNET hSession(NULL);rn HINTERNET hConnect(NULL);rn HINTERNET hRequest(NULL);rnrn wstring strHeaders=L"Content-Type: application/x-www-form-urlencoded\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\n";rnrn if (!ParseUrl(wstrURL.c_str(), strServer, nPort, strObject, nScheme)) rn strResult=ReadErrorMsg("解析URL错误", 0);rn return CONST_SYSTEM_ERROR;rn rnrn // 打开会话rn //LOG_INFO("打开会话...");rn hSession = InternetOpenW( rn L"Mozilla/4.0 (compatible; MSIE 5.22)",rn INTERNET_OPEN_TYPE_PRECONFIG,rn NULL,rn NULL,rn 0rn );rn if (NULL == hSession) rn strResult=ReadErrorMsg("InternetOpen失败", GetLastError());rn return CONST_SYSTEM_ERROR;rn rnrn // 打开连接rn //LOG_INFO("打开连接...");rn hConnect = InternetConnectW(hSession,rn strServer.c_str(),rn nPort,rn NULL,rn NULL,rn INTERNET_SERVICE_HTTP,rn 0,rn NULLrn );rn if (NULL == hConnect ) rn strResult=ReadErrorMsg("InternetConnect失败", GetLastError());rn return CONST_SYSTEM_ERROR;rn rn // 设置请求标志rn //LOG_INFO("设置请求标志...");rn dwFlags = INTERNET_FLAG_RELOAD;rn if (INTERNET_SCHEME_HTTPS == nScheme) rn dwFlags |= INTERNET_FLAG_SECURE;rn dwFlags |= INTERNET_FLAG_IGNORE_CERT_CN_INVALID;rn dwFlags |= INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;rn rnrn //if (!bAutoRedirect) rn dwFlags |= INTERNET_FLAG_NO_AUTO_REDIRECT;rn //rnrnrn // 打开请求rn //LOG_INFO("打开请求...");rn hRequest = HttpOpenRequestW(hConnect,rn wstrVerb.c_str(),rn strObject.c_str(),rn NULL,rn NULL,rn NULL,rn dwFlags,rn /*(DWORD_PTR)this*/rn NULLrn );rn if (NULL == hRequest) rn strResult=ReadErrorMsg("HttpOpenRequest失败", GetLastError());rn return CONST_SYSTEM_ERROR;rn rn /*if (!strCookie.empty()) rn strHeaders += L"Cookie: ";rn strHeaders += strCookie;rn strHeaders += L"\r\n";rn */rnrn LPCWSTR lpszHeaders = strHeaders.empty( ) ? NULL : strHeaders.c_str();rn /*LPVOID lpOptional = strOptional.empty( ) ? NULL : (LPVOID)strOptional.c_str();*/rnrn //LOG_INFO("设置超时...");rn DWORD dwTimeout = AppConfig::GetInstance()->GetTimeout();rn // 设置标志rn InternetSetOption(hRequest, rn INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT,rn &dwTimeout, rn sizeof(DWORD)rn );rn if (INTERNET_SCHEME_HTTPS == nScheme) rn DWORD dwBuffLen = sizeof(dwFlags);rn InternetQueryOption(hRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwBuffLen);rn dwFlags |= SECURITY_FLAG_IGNORE_REVOCATION;rn dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;rn InternetSetOption(hRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, sizeof(dwFlags)) ;rn rn //LOG_INFO("发送请求...");rn bResult = HttpSendRequestW( hRequest,rn lpszHeaders,rn strHeaders.length(),rn lpOptional,rn dwOptionalLengthrn );rnrn if (!bResult) rn if (ERROR_INTERNET_TIMEOUT == GetLastError()) rn //dwStatusCode = HTTP_STATUS_REQUEST_TIMEOUT;rn strResult = "Request timeout!";rnrn elsern strResult=ReadErrorMsg("HttpSendRequestW失败", GetLastError());rn rn return CONST_SYSTEM_ERROR;rn rnrn //LOG_INFO("读取结果...");rn if (bResult) rn //OnReadCookie(hRequest);rn //OnReadStatusCode(hRequest);rn DWORD dwSize = 0;rnrnrn /****************ReadStatusCode***********************/rnrn DWORD dwStatusCode = 0;rn dwSize = sizeof( DWORD );rn if ( !HttpQueryInfo( hRequest,rn HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, rn &dwStatusCode, rn &dwSize,rn NULL rn ) )rn strResult = "读取状态失败";rn return CONST_SYSTEM_ERROR;rn rnrn if(HTTP_STATUS_OK!=dwStatusCode)rn char sz[24] = 0 ;rn sprintf_s( sz, 24, "%d", dwStatusCode );rn strResult = "StatusCode:"+string(sz);rn return CONST_SYSTEM_ERROR;rn rn /****************ReadStatusCode***********************/rnrnrn /****************ReadData***********************/rn LPSTR lpszData =0;rn DWORD dwDownloaded = 0;rn dwSize=0;rnrnrn while ( InternetQueryDataAvailable( hRequest, &dwSize, 0, 0 ) ) rn if ( dwSize == 0 ) rn break;rn rn lpszData = new CHAR[ dwSize+1 ];rn if( InternetReadFile( hRequest,rn (LPVOID)lpszData,rn dwSize,rn &dwDownloaded )rn ) rn strResult += std::string( (char*) lpszData, dwDownloaded );rn rn delete[] lpszData;rn if ( dwDownloaded == 0 ) rn break;rn rn rn //EndRequest();rn strResult=CommonUtil::Utf8ToGBK(strResult);rn /****************ReadData***********************/rn return CONST_NOT_ERROR;rn rn else rn //LOG_ERROR("HttpSendRequest失败", GetLastError());rn //EndRequest();rn rnrn return CONST_SYSTEM_ERROR;rn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试