亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用HTTP實現文本傳輸

發布時間:2021-11-17 14:45:05 來源:億速云 閱讀:188 作者:柒染 欄目:軟件技術

如何使用HTTP實現文本傳輸,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

HTTP協議即超文本傳送協議(Hypertext Transfer Protocol ),是Web聯網的基礎,HTTP協議是建立在TCP協議之上的一種應用。由于HTTP在每次請求結束后都會主動釋放連接,因此HTTP連接是一種短連接。HTTP遵循請求(Request)/應答(Response)模型。所以一定需要實現請求(Gk8HttpRequest)。同時需要實現返回(Gk8HttpResponse)。在游戲研發編程中HTTP請求類型常用的就是GET和POST。框架設計中就需要實現完成HTTP請求和返回的Gk8HttpClient類。HTTP不管是文本傳輸還是二進制傳輸Gk8HttpClient類都能兼容完成響應的功能。HTTP文本傳輸方便簡潔。實現輕量級通信的最佳方式。

libCurl作為是一個多協議的便于客戶端使用的URL傳輸庫,基于C語言,提供C語言的API接口。底層通信都是使用libCurl庫來完成。具體可找相關資料和參閱相關代碼。C語言封裝開發的優勢就是支持跨平臺。Windows平臺,安卓平臺,IOS平臺。跨平臺遷移代碼無需再次開發。一勞永逸的解決通信問題。為了方便開發時HTTP并發和回調。又實現了Gk8HttpServlet類(httpnet目錄中)。高度封裝的代碼目的就是易于理解便于使用。

C++實現網絡HTTP請求類:Gk8HttpRequest.h

#ifndef __GK8HTTPREQUEST_H__
#define __GK8HTTPREQUEST_H__
#pragma once
#include "Gk8BaseObj.h"
#include "Gk8ByteMaker.h"
#include "Gk8Str.h"
typedef GK8_VOID (Gk8BaseObj::*HTTPREQUEST_CALLBACK_FUN)(GK8_LPVOID pHttpResponse);
class Gk8HttpRequest
{
public:
    //[定義HTTP請求類型]
    typedef enum
    {
        kHttpGet,
        kHttpPost,
        kHttpUnkown,
    }HttpRequestType;
protected:
    HttpRequestType m_eRequestType;            //[HTTP請求類型]
    Gk8Str m_sHttpUrl;                        //[HTTP請求地址]
    Gk8ByteMaker m_iRequestData;            //[HTTP請求數據]
    Gk8BaseObj* m_pTarget;                    //[HTTP請求回調目標對象]
    HTTPREQUEST_CALLBACK_FUN m_pSelector;    //[HTTP請求回調函數]
public:
    Gk8HttpRequest()
    {
        m_eRequestType=kHttpUnkown;
        m_sHttpUrl.Empty();
        m_iRequestData.Destroy();
        m_pTarget=NULL;
        m_pSelector=NULL;
    };
    virtual ~Gk8HttpRequest()
    {
        ClearRequestData();
    };
    inline GK8_VOID ClearRequestData()
    {
        m_iRequestData.Destroy();
    }
    inline GK8_VOID SetRequestType(HttpRequestType eRequestType)
    {
        m_eRequestType=eRequestType;
    };
    inline HttpRequestType GetRequestType()
    {
        return m_eRequestType;
    };
    inline GK8_VOID SetHttpUrl(GK8_LPCSTR lpHttpUrl)
    {
        m_sHttpUrl=lpHttpUrl;
    };
    inline GK8_LPCSTR GetHttpUrl()
    {
        return m_sHttpUrl;
    };
    inline GK8_VOID SetRequestData(Gk8ByteMaker* pRequestData)
    {
        pRequestData->ShiftTo(m_iRequestData);
    };
    inline Gk8ByteMaker* GetRequestData()
    {
        return &m_iRequestData;
    }
    inline GK8_INT GetRequestDataSize()
    {
        return m_iRequestData.GetStreamSize();
    }
    inline GK8_VOID SetResponseCallBack(Gk8BaseObj* pTarget,HTTPREQUEST_CALLBACK_FUN pSelector)
    {
        m_pTarget=pTarget;
        m_pSelector=pSelector;
    }
    inline Gk8BaseObj* GetTarget()
    {
        return m_pTarget;
    }
    inline HTTPREQUEST_CALLBACK_FUN GetSelector()
    {
        return m_pSelector;
    }
};
#endif

C++實現網絡HTTP返回類:Gk8HttpResponse.h

#ifndef __GK8HTTPRESPONSE_H__
#define __GK8HTTPRESPONSE_H__
#pragma once
#include "Gk8HttpRequest.h"
class Gk8HttpResponse
{
protected:
    Gk8HttpRequest* m_pHttpRequest;            //[HTTP請求類]
    GK8_BOOL m_bSucceed;                    //[HTTP請求反饋成功]
    Gk8ByteMaker m_iResponseData;            //[HTTP請求反饋數據]
    GK8_INT m_nResponseCode;                //[HTTP請求反饋碼]
    Gk8Str m_sErrorBuffer;                    //[HTTP請求錯誤信息]
protected:
    GK8_BOOL InitWithRequest(Gk8HttpRequest* pHttpRequest);
public:
    Gk8HttpResponse(Gk8HttpRequest* pHttpRequest)
    {
        m_pHttpRequest=pHttpRequest;
        m_bSucceed=false;
        m_iResponseData.Destroy();
        m_sErrorBuffer.Empty();
    }
    virtual ~Gk8HttpResponse()
    {
        if(m_pHttpRequest) delete m_pHttpRequest;
        ClearResponseData();
    }
    inline GK8_VOID ClearResponseData()
    {
        m_iResponseData.Destroy();
    }
    inline Gk8HttpRequest* GetHttpRequest()
    {
        return m_pHttpRequest;
    }
    inline GK8_VOID SetSucceed(GK8_BOOL bSucceed)
    {
        m_bSucceed=bSucceed;
    };
    inline GK8_BOOL IsSucceed()
    {
        return m_bSucceed;
    };
    inline GK8_VOID SetResponseData(Gk8ByteMaker* pResponseData)
    {
        pResponseData->ShiftTo(m_iResponseData);
    }
    inline Gk8ByteMaker* GetResponseData()
    {
        return &m_iResponseData;
    }
    inline GK8_VOID SetResponseCode(GK8_INT nResponseCode)
    {
        m_nResponseCode=nResponseCode;
    }
    inline GK8_INT GetResponseCode()
    {
        return m_nResponseCode;
    }
    inline GK8_VOID SetErrorBuffer(GK8_LPCSTR lpErrorBuffer)
    {
        m_sErrorBuffer=lpErrorBuffer;
    };
    inline GK8_LPCSTR GetErrorBuffer()
    {
        return m_sErrorBuffer;
    }
};
#endif

C++實現HTTP通信類:Gk8HttpClient.h

#ifndef __GK8HTTPCLIENT_H__
#define __GK8HTTPCLIENT_H__
#pragma once
#include "Gk8HttpRequest.h"
#include "Gk8HttpResponse.h"
class Gk8HttpClient
{
private:
    GK8_INT m_nTimeOutForConnect;        //[連接超時時間]
    GK8_INT m_nTimeOutForRead;            //[讀取超時時間]
private:
    Gk8HttpClient();
    virtual ~Gk8HttpClient();
    GK8_BOOL InitThreadSemphore();
public:
    inline GK8_VOID SetTimeoutForConnect(GK8_INT nTimeOutForConnect){m_nTimeOutForConnect=nTimeOutForConnect;};
    inline GK8_INT GetTimeoutForConnect(){return m_nTimeOutForConnect;}
    inline GK8_VOID SetTimeoutForRead(GK8_INT nTimeOutForRead){m_nTimeOutForRead=nTimeOutForRead;};
    inline GK8_INT GetTimeoutForRead(){return m_nTimeOutForRead;};
    GK8_VOID Send(Gk8HttpRequest* pHttpRequest);
    GK8_VOID HttpClientTick();
    static Gk8HttpClient* GetInstance();
};
#endif

C++實現HTTP通信類:Gk8HttpClient.cpp

#include "Gk8HttpClient.h"
#include "curl/curl.h"
#include "Gk8OperSys.h"
#include "Gk8SetMb.cpp"
#include <queue>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include <fcntl.h>
static Gk8HttpClient* sg_pHttpClient=NULL;        //[靜態HTTP客戶端]
static GK8_BOOL sg_bHttpClientQuit=false;        //[退出HTTP]
static sem_t* sg_pSem=NULL;                        //[信號量的數據類型]
static GK8_INT sg_AsyncRequestCount=0;            //[同步請求數]
//[線程及互斥定義]
static pthread_t sg_NetWorkThread;                //[HTTP線程]
static pthread_mutex_t sg_RequestQueueMutex;    //[HTTP請求隊列互斥體]
static pthread_mutex_t sg_ResponseQueueMutex;    //[HTTP響應隊列互斥體]
static Gk8SetMb<Gk8HttpRequest*> sg_iRequestQueue;        //[HTTP請求隊列]
static Gk8SetMb<Gk8HttpResponse*> sg_iResponseQueue;    //[HTTP響應隊列]
static GK8_CHAR sg_szErrorBuf[CURL_ERROR_SIZE];            //[錯誤信息]
static Gk8Str sg_iErrorStr;
typedef size_t (*HTTPWRITE_CALLBACK)(GK8_LPVOID lpData,size_t nSize,size_t nMemBlock,GK8_LPVOID lpResponseData);
#if (GK8_TARGET_PLATFORM==GK8_PLATFORM_IOS)
#define GK8_ASYNC_HTTPREQUEST_USE_NAMED_SEMAPHORE 1
#else
#define GK8_ASYNC_HTTPREQUEST_USE_NAMED_SEMAPHORE 0
#endif
#if GK8_ASYNC_HTTPREQUEST_USE_NAMED_SEMAPHORE
#define GK8_ASYNC_HTTPREQUEST_SEMAPHORE "Gk8HttpAsync"
#else
static sem_t sg_iSem;
#endif
GK8_BOOL ConfigureCURL(CURL* pCurl);
GK8_INT ProcessGetTask(Gk8HttpRequest* pHttpRequest,HTTPWRITE_CALLBACK lpCallBack,GK8_LPVOID lpStream,GK8_LPINT lpResponseCode);
GK8_INT ProcessPostTask(Gk8HttpRequest* pHttpRequest,HTTPWRITE_CALLBACK lpCallBack,GK8_LPVOID lpStream,GK8_LPINT lpResponseCode);
//[接收HTTP數據]
size_t WriteHttpData(GK8_LPVOID lpData,size_t nSize,size_t nMemBlock,GK8_LPVOID lpResponseData)
{
    Gk8ByteMaker* pResponseData=(Gk8ByteMaker*)lpResponseData;
    size_t nLength=nSize*nMemBlock;
    pResponseData->WriteBuf(lpData,(GK8_INT)nLength);
    return nLength;
}
//[設置CURL超時屬性設置]
GK8_BOOL ConfigureCURL(CURL* pCurl)
{
    if(!pCurl) return false;
    CURLcode nCurlCode=curl_easy_setopt(pCurl,CURLOPT_ERRORBUFFER,sg_szErrorBuf);
    if(nCurlCode!=CURLE_OK) return false;
    nCurlCode=curl_easy_setopt(pCurl,CURLOPT_TIMEOUT,Gk8HttpClient::GetInstance()->GetTimeoutForRead());
    if(nCurlCode!=CURLE_OK) return false;
    nCurlCode=curl_easy_setopt(pCurl,CURLOPT_CONNECTTIMEOUT,Gk8HttpClient::GetInstance()->GetTimeoutForConnect());
    if(nCurlCode!=CURLE_OK) return false;
    curl_easy_setopt(pCurl,CURLOPT_SSL_VERIFYPEER,0L);
    curl_easy_setopt(pCurl,CURLOPT_SSL_VERIFYHOST,0L);
    curl_easy_setopt(pCurl,CURLOPT_NOSIGNAL,1L);
    return true;
}
//[處理Get請求]
GK8_INT ProcessGetTask(Gk8HttpRequest* pHttpRequest,HTTPWRITE_CALLBACK lpCallBack,GK8_LPVOID lpStream,GK8_LPINT lpResponseCode)
{
    CURLcode nCurlCode=CURL_LAST;
    CURL* pCurl=curl_easy_init();
    do
    {
        if(!ConfigureCURL(pCurl)) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_URL,pHttpRequest->GetHttpUrl());
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_WRITEFUNCTION,lpCallBack);
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_WRITEDATA,lpStream);
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_FOLLOWLOCATION,true);
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_USERAGENT,"libcurl-agent/1.0");
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_perform(pCurl);
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_getinfo(pCurl,CURLINFO_RESPONSE_CODE,lpResponseCode);
        //[200:指示客服端的請求已經成功收到,解析,接受]
        if(nCurlCode!=CURLE_OK||*lpResponseCode!=200)
        {
            nCurlCode=CURLE_HTTP_RETURNED_ERROR;
        }
    }while(0);
    if(pCurl) curl_easy_cleanup(pCurl);
    return (nCurlCode==CURLE_OK?0:1);
}
//[處理POST請求:流的行事發出]
GK8_INT ProcessPostTask(Gk8HttpRequest* pHttpRequest,HTTPWRITE_CALLBACK lpCallBack,GK8_LPVOID lpStream,GK8_LPINT lpResponseCode)
{
    CURLcode nCurlCode=CURL_LAST;
    CURL* pCurl=curl_easy_init();
    do
    {
        if(!ConfigureCURL(pCurl)) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_URL,pHttpRequest->GetHttpUrl());
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_WRITEFUNCTION,lpCallBack);
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_WRITEDATA,lpStream);
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_POST,1);
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_POSTFIELDS,pHttpRequest->GetRequestData()->GetBuf());
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_setopt(pCurl,CURLOPT_POSTFIELDSIZE,pHttpRequest->GetRequestDataSize());
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_perform(pCurl);
        if(nCurlCode!=CURLE_OK) break;
        nCurlCode=curl_easy_getinfo(pCurl,CURLINFO_RESPONSE_CODE,lpResponseCode);
        if(nCurlCode!=CURLE_OK||*lpResponseCode!=200)
        {
            nCurlCode=CURLE_HTTP_RETURNED_ERROR;
        }
    }while(0);
    if(pCurl) curl_easy_cleanup(pCurl);
    return (nCurlCode==CURLE_OK?0:1);
}
//[創建網絡線程]
static GK8_LPVOID NetWorkThread(GK8_LPVOID lpData)
{
    Gk8HttpRequest* pHttpRequest=NULL;
    Gk8HttpResponse* pHttpResponse=NULL;
    while(true)
    {
        //Wait for http request tasks from main thread
        GK8_INT nSemWaitRet=sem_wait(sg_pSem);
        if(nSemWaitRet<0)
        {
            _GK8ERR<<"HttpRequest async thread semaphore error:"<<strerror(errno)<<CR;
            break;
        }
        if(sg_bHttpClientQuit) break;
        //[第一步:發送HTTP請求]
        pHttpRequest=NULL;
        pthread_mutex_lock(&sg_RequestQueueMutex);
        if(0!=sg_iRequestQueue.GetSize())
        {
            pHttpRequest=sg_iRequestQueue.GetItemAt(0);
            sg_iRequestQueue.RemoveItemAt(0);
        }
        pthread_mutex_unlock(&sg_RequestQueueMutex);
        if(NULL==pHttpRequest) continue;
        //[第二步:libCurl同步請求]
        pHttpResponse=new Gk8HttpResponse(pHttpRequest);
        GK8_INT nResponseCode=-1;
        GK8_INT nRetValue=0;
        switch(pHttpRequest->GetRequestType())
        {
            case Gk8HttpRequest::kHttpGet:
                nRetValue=ProcessGetTask(pHttpRequest,WriteHttpData,pHttpResponse->GetResponseData(),&nResponseCode);
                break;
            case Gk8HttpRequest::kHttpPost:
                nRetValue=ProcessPostTask(pHttpRequest,WriteHttpData,pHttpResponse->GetResponseData(),&nResponseCode);
                break;
            default:
                _GK8ERR<<"Gk8HttpClient:Unkown Request Type,Only GET And POST Are Supported"<<CR;
                break;
        }
        pHttpResponse->SetResponseCode(nResponseCode);
        //[清除請求中的二進制數據]
        pHttpRequest->ClearRequestData();
        if(nRetValue!=0)
        {
            pHttpResponse->SetSucceed(false);
            sg_iErrorStr=sg_szErrorBuf;
            pHttpResponse->SetErrorBuffer(sg_iErrorStr);
        }else
        {
            pHttpResponse->SetSucceed(true);
        }
        pthread_mutex_lock(&sg_ResponseQueueMutex);
        sg_iResponseQueue.AddItem(pHttpResponse);
        pthread_mutex_unlock(&sg_ResponseQueueMutex);
    }
    sg_AsyncRequestCount-=sg_iRequestQueue.GetSize();
    pthread_mutex_lock(&sg_RequestQueueMutex);
    sg_iRequestQueue.Clear();
    pthread_mutex_unlock(&sg_RequestQueueMutex);
    if(sg_pSem!=NULL)
    {
#if GK8_ASYNC_HTTPREQUEST_USE_NAMED_SEMAPHORE
        sem_unlink(GK8_ASYNC_HTTPREQUEST_SEMAPHORE);
        sem_close(sg_pSem);
#else
        sem_destroy(sg_pSem);
#endif
        sg_pSem=NULL;
        pthread_mutex_destroy(&sg_RequestQueueMutex);
        pthread_mutex_destroy(&sg_ResponseQueueMutex);
        //[依次刪除數據]
        GK8_INT nIndex;
        for(nIndex=0;nIndex<sg_iRequestQueue.GetSize();nIndex++)
        {
            pHttpRequest=sg_iRequestQueue.GetItemAt(nIndex);
            delete pHttpRequest;
        }
        for(nIndex=0;nIndex<sg_iResponseQueue.GetSize();nIndex++)
        {
            pHttpResponse=sg_iResponseQueue.GetItemAt(nIndex);
            delete pHttpResponse;
        }
        sg_iRequestQueue.Destroy();
        sg_iResponseQueue.Destroy();
    }
    pthread_exit(NULL);
    return 0;
}
/////////////////////////////////////////////////////////////////
Gk8HttpClient::Gk8HttpClient()
:m_nTimeOutForRead(60)
,m_nTimeOutForConnect(30)
{
}
Gk8HttpClient::~Gk8HttpClient()
{
    sg_bHttpClientQuit=true;
    if(sg_pSem!=NULL)
    {
        sem_post(sg_pSem);
    }
}
//[初始化線程]
GK8_BOOL Gk8HttpClient::InitThreadSemphore()
{
    if(sg_pSem!=NULL)
    {
        return true;
    }else
    {
#if GK8_ASYNC_HTTPREQUEST_USE_NAMED_SEMAPHORE
        sg_pSem=sem_open(GK8_ASYNC_HTTPREQUEST_SEMAPHORE,O_CREAT,0644,0);
        if(sg_pSem==SEM_FAILED)
        {
            _GK8ERR<<"Open HttpRequest Semaphore Failed"<<CR;
            sg_pSem=NULL;
            return false;
        }
#else
        GK8_INT nSemRet=sem_init(&sg_iSem,0,0);
        if(nSemRet<0)
        {
            _GK8ERR<<"Init HttpRequest Semaphore Failed"<<CR;
            return false;
        }
        sg_pSem=&sg_iSem;
#endif
        pthread_mutex_init(&sg_RequestQueueMutex,NULL);
        pthread_mutex_init(&sg_ResponseQueueMutex,NULL);
        pthread_create(&sg_NetWorkThread,NULL,NetWorkThread,NULL);
        pthread_detach(sg_NetWorkThread);
        sg_bHttpClientQuit=false;
    }
    return true;
}
//[發送HTTP請求]
GK8_VOID Gk8HttpClient::Send(Gk8HttpRequest* pHttpRequest)
{
    if(false==InitThreadSemphore()) return;
    if(!pHttpRequest) return;
    ++sg_AsyncRequestCount;
    pthread_mutex_lock(&sg_RequestQueueMutex);
    sg_iRequestQueue.AddItem(pHttpRequest);
    pthread_mutex_unlock(&sg_RequestQueueMutex);
    sem_post(sg_pSem);
}
//[獲取HTTP單例]
Gk8HttpClient* Gk8HttpClient::GetInstance()
{
    if(sg_pHttpClient==NULL) sg_pHttpClient=new Gk8HttpClient();
    return sg_pHttpClient;
}
//[HTTP響應幀控制]
GK8_VOID Gk8HttpClient::HttpClientTick()
{
    if(sg_pSem==NULL) return;
    Gk8HttpResponse* pHttpResponse=NULL;
    pthread_mutex_lock(&sg_ResponseQueueMutex);
    if(sg_iResponseQueue.GetSize()>0)
    {
        pHttpResponse=sg_iResponseQueue.GetItemAt(0);
        sg_iResponseQueue.RemoveItemAt(0);
    }
    pthread_mutex_unlock(&sg_ResponseQueueMutex);
    if(pHttpResponse)
    {
        --sg_AsyncRequestCount;
        Gk8HttpRequest* pHttpRequest=pHttpResponse->GetHttpRequest();
        Gk8BaseObj* pTarget=pHttpRequest->GetTarget();
        HTTPREQUEST_CALLBACK_FUN pSelector=pHttpRequest->GetSelector();
        //[回調處理:記錄數據]
        if(pTarget&&pSelector)
        {
            (pTarget->*pSelector)(pHttpResponse);
        }
        //[銷毀請求數據]
        delete pHttpResponse;
    }
}

C++實現Servlet類:Gk8HttpServlet.h

#ifndef __GK8HTTPSERVICE_H__
#define __GK8HTTPSERVICE_H__
#pragma once
#include "curl/curl.h"
#include "Gk8HttpClient.h"
#include "Gk8HttpRequest.h"
#include "Gk8HttpResponse.h"
class Gk8HttpServlet:public Gk8BaseObj
{
    DECLARE_TOSPP_MAP;
private:
    Gk8Str m_sHttpUrl;                //[HTTP請求地址]
    Gk8ByteMaker m_iRequestData;    //[HTTP請求數據]
    Gk8Var m_iHttpCallVar;            //[HTTP回調信息]
    GK8_VOID ServletRequestCompleted(GK8_LPVOID lpHttpResponse);
public:
    Gk8HttpServlet();
    ~Gk8HttpServlet();
    GK8_VOID TOSPPFUNC SendHttpMessage(GK8_LPCSTR lpHttpData,Gk8Var& iHttpCallVar);
};
#endif

C++實現Servlet類:Gk8HttpServlet.cpp

#include "Gk8HttpServlet.h"
#include "Gk8OperSys.h"
#include "Gk8Helper.h"
static Gk8Str sg_iReceiveMessageEvent("OnReceiveMessage");//[接收網絡信息]
#define HTTPSERVLET_FAIL        0        //[HTTP請求失敗]
#define HTTPSERVLET_SUCC        1        //[HTTP請求成功]
/////////////////////////////////////////////CLASS-TOLUA////////////////////////////////////////////////////
TOLUA_CLASS_COLLECT_FUNC(Gk8HttpServlet)
BEGIN_TOLUA_CLASS_FUNC(Gk8HttpServlet,NewObj)
    if(!CheckToLuaFunParam(L,"NewObj",Gk8Var()<<ISUSERTABLE("Gk8HttpServlet")<<ISVABOOLEAN<<ISNOOBJ)) return 0;
    Gk8HttpServlet* pHttpServlet=(Gk8HttpServlet*)Mtolua_new((Gk8HttpServlet)());
    tolua_pushusertype(L,(GK8_LPVOID)pHttpServlet,"Gk8HttpServlet");
    if(IFBOOLEAN(2)) tolua_register_gc(L,lua_gettop(L));
END_TOLUA_CLASS_FUNC
BEGIN_TOLUA_CLASS_FUNC(Gk8HttpServlet,SendHttpMessage)
    if(!CheckToLuaFunParam(L,"SendHttpMessage",Gk8Var()<<ISUSERTYPE("Gk8HttpServlet")<<ISSTRING<<ISVAFUNCTION<<ISNOOBJ)) return 0;
    //TOLUAGETOBJ(Gk8HttpServlet,1)->SendHttpMessage(TOLUAGETSTRING(2),TOLUAGETFUNEX(L,3,0));
END_TOLUA_CLASS_FUNC
//[啟動注冊類的全部TOLUA函數]
BEGIN_TOLUA_FUN_MAP(Gk8HttpServlet)
    TOLUA_CLASS(Gk8HttpServlet,Gk8BaseObj,"[網絡類]","[HTTP請求類]")
    TOLUA_CLASS_FUNC(Gk8HttpServlet,NewObj,"[Gk8HttpServlet* NewObj(GK8_BOOL ifLocal=false)?創建Gk8HttpServlet對象]")
    TOLUA_CLASS_FUNC(Gk8HttpServlet,SendHttpMessage,"[GK8_VOID SendHttpMessage(GK8_LPCSTR lpHttpData,iHttpCallVar)?發送HTTP請求,字符串模式]")
END_TOLUA_FUN_MAP
/////////////////////////////////////////////CLASS-TOSPP////////////////////////////////////////////////////
BEGIN_TOSPP_MAP(Gk8HttpServlet,Gk8BaseObj)
    TOSPP_FUNC(Gk8HttpServlet,SendHttpMessage,' ',"s&v","SendHttpMessage(lpHttpData,iHttpCallVar)")
END_TOSPP_MAP()
/////////////////////////////////////////////////////////////////////////////////////////////////
Gk8HttpServlet::Gk8HttpServlet()
{
}
Gk8HttpServlet::~Gk8HttpServlet()
{
}
//[發送HTTP請求注意編碼問題]
GK8_VOID Gk8HttpServlet::SendHttpMessage(GK8_LPCSTR lpHttpData,Gk8Var& iHttpCallVar)
{
    m_sHttpUrl=lpHttpData;
    //[注冊腳本回調消息]
    m_iHttpCallVar=iHttpCallVar;
    Gk8Var iEmptyVar;
    if(m_iHttpCallVar.GetSize()==1 && m_iHttpCallVar[0].IfInt()) SetEvent(sg_iReceiveMessageEvent,m_iHttpCallVar[0],iEmptyVar);
    Gk8HttpRequest* pHttpRequest=new Gk8HttpRequest();
    pHttpRequest->SetRequestType(Gk8HttpRequest::kHttpGet);
    pHttpRequest->SetHttpUrl(m_sHttpUrl);
    pHttpRequest->SetResponseCallBack(this,(HTTPREQUEST_CALLBACK_FUN)&Gk8HttpServlet::ServletRequestCompleted);
    pHttpRequest->SetRequestData(&m_iRequestData);
    Gk8HttpClient::GetInstance()->Send(pHttpRequest);
}
//[HTTP請求信息回調]
GK8_VOID Gk8HttpServlet::ServletRequestCompleted(GK8_LPVOID lpHttpResponse)
{
    Gk8HttpResponse* pHttpResponse=(Gk8HttpResponse*)lpHttpResponse;
    if(!pHttpResponse) return;
    //[把服務器數據派遣到腳本中]
    GK8_INT nFailCode=HTTPSERVLET_SUCC;
    Gk8Str iResponstStr;
    if(!pHttpResponse->IsSucceed())
    {
        _GK8ERR<<"Gk8HttpServlet Response Failed Error Is "<<pHttpResponse->GetErrorBuffer()<<CR;
        iResponstStr="UnKown Error";
        nFailCode=HTTPSERVLET_FAIL;
    }else
    {
        Gk8ByteMaker* pResponseData=pHttpResponse->GetResponseData();
        iResponstStr.BinToStr(pResponseData->GetBuf(),pResponseData->GetStreamSize());
    }
    if(m_iHttpCallVar.GetSize()==1)
    {
        RunEventWithArgs(sg_iReceiveMessageEvent,iResponstStr);
    }else
    {
        if(!m_iHttpCallVar[0].IfPtr()||!IfObjPtr(m_iHttpCallVar[0].GetPtr(),m_iHttpCallVar[0].GetPtrId())) return;
        Gk8Obj* pBindObj=m_iHttpCallVar[0].GetSafePtr();
        Gk8Var iParamVar;
        iParamVar<<nFailCode<<iResponstStr;
        pBindObj->OnCall(m_iHttpCallVar[1],iParamVar);
    }
    //[直接刪除本身]
    SafeDeleteObj(this);
}

看完上述內容,你們掌握如何使用HTTP實現文本傳輸的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

临汾市| 滨海县| 中卫市| 卓资县| 新津县| 沅江市| 炉霍县| 大英县| 固镇县| 虎林市| 灵台县| 江源县| 固安县| 井冈山市| 长白| 汝阳县| 江北区| 准格尔旗| 新巴尔虎右旗| 贵溪市| 合作市| 边坝县| 湟中县| 阿坝| 清河县| 梅河口市| 进贤县| 昌邑市| 古丈县| 兴化市| 民和| 曲松县| 广水市| 邓州市| 吉隆县| 龙南县| 阳城县| 策勒县| 太和县| 什邡市| 南皮县|