Issue
This Content is from Stack Overflow. Question asked by Veerkaran Singh
So I am trying to use this https://perpetualprogrammers.wordpress.com/2016/05/22/the-http-server-api/
and it works fine if i use xampp and do this locally on same pc from php
$ch = curl_init("http://localhost:9000/api/cpp/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
echo $data;
curl_close($ch);
but if i upload the code to a website then it doesnt even connect with my http server api and code is
ULONG result = 0;
HTTPAPI_VERSION version = HTTPAPI_VERSION_2;
result = HttpInitialize(version, HTTP_INITIALIZE_SERVER, 0);
HTTP_SERVER_SESSION_ID serverSessionId;
result = HttpCreateServerSession(version, &serverSessionId, 0);
HTTP_URL_GROUP_ID groupId;
result = HttpCreateUrlGroup(serverSessionId, &groupId, 0);
HANDLE requestQueueHandle;
result = HttpCreateRequestQueue(version, NULL, NULL, 0, &requestQueueHandle);
HTTP_BINDING_INFO info;
info.Flags.Present = 1;
info.RequestQueueHandle = requestQueueHandle;
result = HttpSetUrlGroupProperty(groupId, HttpServerBindingProperty, &info, sizeof(info));
PCWSTR url = (argc == 2) ? argv[1] : L"http://localhost:9000/api/cpp/";
result = HttpAddUrlToUrlGroup(groupId, url, 0, 0);
wprintf(L"Listening. Please submit requests to: %sn", url);
for (;;)
{
HTTP_REQUEST_ID requestId = 0;
HTTP_SET_NULL_ID(&requestId);
int bufferSize = 4096;
int requestSize = sizeof(HTTP_REQUEST) + bufferSize;
BYTE *buffer = new BYTE[requestSize];
PHTTP_REQUEST pRequest = (PHTTP_REQUEST)buffer;
RtlZeroMemory(buffer, requestSize);
ULONG bytesReturned;
result = HttpReceiveHttpRequest(
requestQueueHandle,
requestId,
HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY,
pRequest,
requestSize,
&bytesReturned,
NULL
);
wprintf(L"Full URL: %wsn", pRequest->CookedUrl.pFullUrl);
wprintf(L" Path: %wsn", pRequest->CookedUrl.pAbsPath);
if (pRequest->Verb == HttpVerbDELETE)
{
wprintf(L"Asked to stop.n");
break;
}
// Respond to the request.
HTTP_RESPONSE response;
RtlZeroMemory(&response, sizeof(response));
response.StatusCode = 200;
response.pReason = "OK";
response.ReasonLength = (USHORT)strlen(response.pReason);
response.Headers.KnownHeaders[HttpHeaderContentType].pRawValue = "text/html";
response.Headers.KnownHeaders[HttpHeaderContentType].RawValueLength = (USHORT)strlen(response.Headers.KnownHeaders[HttpHeaderContentType].pRawValue);
PSTR pEntityString = "Hello from C++";
HTTP_DATA_CHUNK dataChunk;
dataChunk.DataChunkType = HttpDataChunkFromMemory;
dataChunk.FromMemory.pBuffer = pEntityString;
dataChunk.FromMemory.BufferLength = (ULONG)strlen(pEntityString);
response.EntityChunkCount = 1;
response.pEntityChunks = &dataChunk;
result = HttpSendHttpResponse(
requestQueueHandle,
pRequest->RequestId,
0,
&response,
NULL,
NULL, // &bytesSent (optional)
NULL,
0,
NULL,
NULL
);
delete buffer;
}
result = HttpRemoveUrlFromUrlGroup(groupId, url, 0);
info.Flags.Present = 0;
info.RequestQueueHandle = NULL;
result = HttpSetUrlGroupProperty(groupId, HttpServerBindingProperty, &info, sizeof(info));
result = HttpShutdownRequestQueue(requestQueueHandle);
result = HttpTerminate(HTTP_INITIALIZE_SERVER, NULL);
I have looked for the fix but I couldn’t find it so I asked here hopefully someone can fix it.
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.