Skip to content

Commit

Permalink
* change README
Browse files Browse the repository at this point in the history
  • Loading branch information
YuHuanTin committed Feb 3, 2023
1 parent b4b9d54 commit 2a2234d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

![image](https://learn.microsoft.com/en-us/windows/win32/winhttp/images/art-winhttp3.png)
## Import Methods:
##### main.cpp
##### CMakeLists.txt:
```c++
#include "..\\WinhttpAPI.h"
include_directories(${PROJECT_SOURCE_DIR}/include)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/lib/Winhttp_SimpleAPI.lib)
```
##### CMakeLists.txt:
##### main.cpp
```c++
link_directories(lib) #the directory where the libs are stored, as defined by you. like ..\\lib
link_libraries(Winhttp_SimpleAPI.lib)
#include <WinhttpAPI.h>
```
##### Or main.cpp:
```c++
Expand All @@ -25,22 +26,19 @@ httpRequest.url = "https://www.baidu.com";
httpRequest.protocol = "get";

WinhttpAPI httpAPI;
httpAPI.SetHeader(httpRequest,"Connection","keep-alive");
httpAPI.SetHeader(httpRequest,"Context-Type","text/html");
httpAPI.SetHeader("Connection","keep-alive");
httpAPI.SetHeader("Context-Type","text/html");
httpAPI.Request(httpRequest,httpResponse);

//Get the content and headers returned from a web visit.
printf("[+]%s,%s\n",httpResponse.body.c_str(),httpResponse.headers.c_str());
std::cout << "[+]" << httpResponse.body << ", " << httpResponse.headers << '\n';
//Retrieve a specific key value from the returned headers
printf("[x]%s\n",httpAPI.GetHeader("Date").c_str());
std::cout << "[+]" << httpAPI.GetHeader("Date") << '\n';
```
##### Request a large file
##### Download a file
```c++
HttpRequestT httpRequest;
HttpRequestT httpRequest = {"https://example.com/1GB.bin", "get", {HttpRequestT::SaveMethodT::FILE_STREAM, "C:\\test.bin"}};
HttpResponseT httpResponse;
httpRequest.url = "http://test.com/5Gb.bin";//example file url
httpRequest.protocol = "get";
httpRequest.SaveMethod = {HttpRequestT::SaveMethodT::FILE_STREAM, "C:\\5.bin"};

WinhttpAPI httpAPI(httpRequest,httpResponse);
httpAPI.request();
Expand Down
22 changes: 11 additions & 11 deletions WinhttpAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,29 @@ void WinhttpAPI::Request(HttpRequestT &HttpRequest, HttpResponseT &HttpResponse)
HttpResponse.Headers.clear();
}

//Declare
// Declare
DWORD dwFlag = 0;
WinHttpInterface httpInterface;

//Open
// Open
wstring wszUA = CodeCvt::StrToWstr(ParamProcess::GetUA(this->headers), CP_ACP);
wstring wszProxy = CodeCvt::StrToWstr(HttpRequest.proxy, CP_ACP);
wstring wszProxyBypass = CodeCvt::StrToWstr(HttpRequest.proxyBypass, CP_ACP);
if ((lastError = httpInterface.Open(wszUA, wszProxy, wszProxyBypass, 0)) != 0)
throw runtime_error("failed Open");

//Crack
// Crack
wstring wszUrl = CodeCvt::StrToWstr(HttpRequest.url, CP_ACP);
URL_COMPONENTS UrlComponents = ParamProcess::InitUrlComponents();
if ((lastError = httpInterface.CrackUrl(wszUrl, wszUrl.length(), 0, UrlComponents)) != 0)
throw runtime_error("failed CrackUrl");

//Connect
// Connect
wstring wszUrlHostName(&UrlComponents.lpszHostName[0], &UrlComponents.lpszHostName[UrlComponents.dwHostNameLength]);
if ((lastError = httpInterface.Connect(wszUrlHostName, UrlComponents.nPort)) != 0)
throw runtime_error("failed Connect");

//OpenRequest
// OpenRequest
wstring wszModel = CodeCvt::StrToWstr(HttpRequest.protocol, CP_ACP);
switch (UrlComponents.nScheme) {
case INTERNET_SCHEME_HTTP:
Expand All @@ -67,30 +67,30 @@ void WinhttpAPI::Request(HttpRequestT &HttpRequest, HttpResponseT &HttpResponse)
if ((lastError = httpInterface.OpenRequest(wszModel, UrlComponents.lpszUrlPath, L"", dwFlag)) != 0)
throw runtime_error("failed OpenRequest");

//SetTimeOut
// SetTimeOut
httpInterface.SetTimeOut(HttpRequest.timeout.resolveTimeout, HttpRequest.timeout.connectTimeout,
HttpRequest.timeout.sendTimeout, HttpRequest.timeout.receiveTimeout);

//SetOption
// SetOption
if (HttpRequest.winhttpOption.dwOption != 0)
httpInterface.SetOption(HttpRequest.winhttpOption.dwOption, HttpRequest.winhttpOption.lpBuffer);

//AddRequestHeaders
// AddRequestHeaders
for (const auto &ch: this->headers) {
wstring header = (CodeCvt::StrToWstr(ch.first + ": " + ch.second + "\r\n", CP_ACP));
httpInterface.AddRequestHeaders(header, 0);
}

//SendRequest
// SendRequest
if ((lastError = httpInterface.SendRequest(L"", HttpRequest.body, HttpRequest.body.length(),
HttpRequest.body.length())) != 0)
throw runtime_error("failed SendRequest");

//ReceiveResponse
// ReceiveResponse
if ((lastError = httpInterface.ReceiveResponse()) != 0)
throw runtime_error("failed ReceiveResponse");

//QueryHeaders
// QueryHeaders
DWORD dwBufLen = httpInterface.QueryHeaders(nullptr, 0);
if (dwBufLen > 0) {
std::unique_ptr<wchar_t[]> wBuf = std::make_unique<wchar_t[]>(dwBufLen);
Expand Down
4 changes: 2 additions & 2 deletions WinhttpAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ struct HttpRequestT {

HttpRequestT() : saveMethod(SaveMethodT::MethodE::STRING_STREAM) {}

HttpRequestT(std::string Url, std::string Method, SaveMethodT SaveMethod = {SaveMethodT::STRING_STREAM})
: url(std::move(Url)), protocol(std::move(Method)), saveMethod(std::move(SaveMethod)) {
HttpRequestT(std::string Url, std::string Protocol, SaveMethodT SaveMethod = {SaveMethodT::STRING_STREAM})
: url(std::move(Url)), protocol(std::move(Protocol)), saveMethod(std::move(SaveMethod)) {
}
};
struct HttpResponseT {
Expand Down

0 comments on commit 2a2234d

Please sign in to comment.